How to force lazy-OR evaluation in VB.NET? - vb.net

I am reading in data from a database and putting it in a DataRow.
I want to test a nullable boolean field for whether it is Null or True.
How can I do the following in VB.NET without it throwing an exception about how the Or operator cannot have DBNull as one of its operands? Can I force lazy-OR evalution so that is just evaluates the IsNull and proceeds no further?
If row.IsNull("MyNullableBooleanField") Or row("MyNullableBooleanField")

Use the 'OrElse' operator:
If row.IsNull("MyNullableBooleanField") OrElse row("MyNullableBooleanField") Then

Related

Null conditional operator not working (shows "invoke is not a member of xxx")

I can't seem to find anyone else having this same issue. I have seen that you can use null-conditional operators in VB.NET. E.g.
SendNews?.Invoke("Just in: A newsworthy item...")
However, I'm getting "invoke is not a member of SendNews"
I have tried setting "Option Infer On" but still getting this error.
Any ideas? (using .net 4.6.1)
The null conditional operator is used in place of a null check. For instance, instead of this:
If SendNews IsNot Nothing Then
SendNews.Invoke("...")
End If
You can shorten it to this:
SendNews?.Invoke("...")
But, that's all it does. It does nothing to check if the object actually contains the members being accessed. If you are using late binding (Dim SendNews As Object with Option Strict Off, the rough equivalent of using dynamic in C#), the null conditional operator will not skip the method call if it doesn't exist on the object. It will still throw the same exception as it would have otherwise. Currently, the only way to check if a late-bound object contains a particular method is to catch and ignore the exception or to look for it, by string name, with reflection.

Conditional Statement Returns Exception Error

I have this conditional statement
loRecordFieldData = CType(IIf(loRecordsAttributeCollection.ContainsKey(loMappingObject.FieldID), _
loRecordsAttributeCollection(loMappingObject.FieldID), Nothing)
the problem is when loRecordsAttributeCollection doesn't contain the FieldID it return exception error key not found instead of nothing.
Could anyone explain this and how to prevent it?
Thank you
IIf is a function, so all of its arguments will be evaluated before it’s called. Use If instead, which is a genuine inline conditional and won’t evaluate the operand that isn’t returned:
loRecordFieldData = CType(If(loRecordsAttributeCollection.ContainsKey(loMappingObject.FieldID), _
loRecordsAttributeCollection(loMappingObject.FieldID), Nothing)
If this is a dictionary, you can also use TryGetValue or wrap it in an extension method:
Dim loRecordFieldData As … ' the value type of loRecordsAttributeCollection
loRecordsAttributeCollection.TryGetValue(loMappingObject.FieldID, loRecordFieldData)
' now cast
Use If instead. IIf syntax is obsolete in vb.net

How to use ABAP boolean in IF condition?

I know abap has no real boolean type. Instead 'X' and ' ' is used. Up to this time I always used an if-statement that way:
IF myObj->is_sth( ) = abap_true.
ENDIF.
Now I did something like this:
IF myObj->is_sth( ).
ENDIF.
And I'm wondering that this seems to work. Return Type is boolean. I'm on Netweaver 7.4. Can I use this without problems? It's like my lovely C# writing :p.
This is called a predicative method call:
A predicative method call is a relational expression whose only
operand is a functional method call meth( ... ). The result of the
relational expression is true if the result of the functional method
call is not initial and false if the result of the functional method
call is initial. The result of the functional method call (the return
value of the called function method) can have any data type. A check
is made on the type-friendly initial value.
A predicative method call, like any relational expression, can be a
full logical expression or part of a logical expression. This means it
can be specified as a condition in control statements and other
statements, as an argument in Boolean functions or conditional
expressions, or in joins with Boolean operators.
This was introduced in 7.40 SP08. Be aware that this only works reliably if the initial value is false and false is the initial value. For instance, IS-H uses a character field where 0 is false and 1 is true - but since the initial value of a character field is a space, that's neither true nor false, so using any method that returns this value will always branch as if the method had returned true...

VB.NET LINQ Method Syntax disallows implicit conversions from 'Integer?' to 'Integer'

Compare weirdness when working with LINQ and Entity Framework.
I want to retrieve an ID from my DB and I get this weird message.
I could simply fix it as you can see but I want to understand why this happens.
Question:
Why do I get this error message even if I check with "HasValue" or I use "FirstOrDefault"? It can't be null in my opinion but I obviously miss something.
Add .Value if you are 100% sure the Integer? has a value.
Why do I get this error message even if I check with "HasValue"
Entity Framework just uses the objects you give it. It can't create a new object where OPX_ isn't nullable.
the setOpxRights function presumably takes an Integer as a parameter and Option Strict On won't allow an Integer? to be implicitly converted to an Integer. If you are sure that it will always have a value, pass in cctUser.OPX_Rechte.Value
The compiler is not perfect, we can see that OPX_Rechte will have a value because of the where statment, but for the compiler you are just using a the object cctUser that have an Integer? and it needs an Integer.

Is the 'Is' VB.NET keyword the same as Object.ReferenceEquals?

Is the Is VB.NET keyword the same as Object.ReferenceEquals?
Yes, it is, unless combined with a TypeOf check.
Quote from MSDN:
The Is operator determines if two
object references refer to the same
object. However, it does not perform
value comparisons. If object1 and
object2 both refer to the exact same
object instance, result is True; if
they do not, result is False.
Is can also be used with the TypeOf
keyword to make a TypeOf...Is
expression, which tests whether an
object variable is compatible with a
data type.
BTW, also note the IsNot operator (which gives the boolean inverse of the matching Is expression):
IsNot is the opposite of the Is
operator. The advantage of IsNot is
that you can avoid awkward syntax with
Not and Is, which can be difficult to
read.