VB.NET null coalescing operator? [duplicate] - vb.net

This question already has answers here:
Coalesce operator and Conditional operator in VB.NET [duplicate]
(4 answers)
Closed 2 years ago.
Possible Duplicates:
Coalesce operator and Conditional operator in VB.NET
Is there a VB.NET equivalent for C#'s ?? operator?
Is there a built-in VB.NET equivalent to the C# null coalescing operator?

Yes, there is, a long as you're using VB 9 or later (included with Visual Studio 2008).
You can use the version of the If operator overloaded to accept only two arguments:
Dim myVar? As Integer = Nothing
Console.WriteLine(If(myVar, 7))
More information can be found here in a blog post by the VB.NET team.
(Yes, this is an operator, even though it looks like a function. It will compile down to the same IL as the "proper" null-coalescing operator in C#.)
Example
Dim b As Boolean?
Console.WriteLine("{0}.", If(b, "this is expected when b is nothing"))
'output: this is expected when b is nothing.
b = False
Console.WriteLine("{0}.", If(b, "this is unexpected when b is false"))
'output: False.
b = True
Console.WriteLine("{0}.", If(b, "this is unexpected when b is true"))
'output: True.

According to this question it would seem the answer is If()

No. Use GetValueOrDefault; that's why it's there!

I don't believe that there is a built in VB.Net equivalent, but here's an answer: null coalesce operator in VB.Net(8)

Related

What is the “and” and “or” Operator in Kotlin?

I used an || operator within the Kotlin IDEA but was throwing an error. This confused me, one of the first queries when searching google was a closed stack overflow thread with a snarky "answer" comment which wasn't helpful.
The first query in google hit is some function "or" gibberish.
My code:
if(inputAmount >= 0 || inputAmount = -99)
I understand what is "wrong". there was some logic errors the second part of the "if" statement should have been inputAmount == -99. In my case, the code needed to further be adjusted because of the actual type that was being used.
if(inputAmount >= 0.0 || inputAmount.toInt() == -99)
This appears to be different then other languages in that other languages just simply allow you to have your "logic" error with the "inputAmount = -99". So the '||' operator is allowed and is similar to most other languages.
So first step if encounter this error is to make sure your logic is correct. (check)
infix functions > sense according to the documentation "or" and "and" are infix functions that don't use the short circuit, is it technically wrong to call the "||" operator an "or" operator and should be called logical 'or'?
when referring to the infix 'or' how do people refer to that in Kotlin?
in boolean logic takes statements. A=b is a statement that is always true
No, it isn't. In C, C++ and Java it's an expression, and its value is the value of b. If b is false, it's false, if b is 10, it's 10. And you really don't want to confuse "statements" in programming languages with "statements" in logic; they are entirely different things.
Separately, C and C++ (but not Java) allow || to work on all integral types, and not just booleans (because they didn't originally have booleans as a separate type).
when referring to the infix function 'or' how do Kotlin folk typically refer to that?
Bitwise or for integral types, and I've never actually seen anyone use non-short-circuiting or on booleans, but if I had to I'd call it... well, non-short-circuiting or.
is it technically wrong to call the "||" operator an "or" operator and should be called logical 'or'
Both || and or (on booleans) are "logical 'or'" and I don't see any problem with calling them simply 'or'. If you need to distinguish use short-circuiting vs non-short-circuiting. But again, I've never actually ran into a use of the non-short-circuiting version.

Why And operator in vb.net

I always use AndAlso while checking multiple conditions as it doesn't evaluate right side unless left one is true. I don't see any situation where someone would like to evaluate right side even if left one fails. If it was needed then why they didn't include same in C#.
Update:
As accepted answer pointed out that it exists because it is used for bitwise operation, that fine enough but I still think they would have overloaded And operator to serve both purposes and just not created AndAlso. If anyone can pour some light on it, this question is still open :)
They included the same in C#. In C# you can use & (And) or && (AndAlso).
There's no real use case i can imagine for the not short-circuit operator when comparing booleans, but And can be used with numeric values, and it then does a bitwise comparison. That's why it exists. But when comparing boolean types, you'll always be using the short-circuit version.
And is also a bit operator. Here is an example showing a mix of And an AndAlso.
Dim foo? As Integer = 5
If foo.HasValue AndAlso (foo And 1) = 1 AndAlso (foo And 4) = 4 Then
Stop
End If

Does an 'if' statement always evaluate all conditions? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does Objective-C use short-circuit evaluation?
If an object is of a certain type, and a property of that object has a certain value, I want to do something.
Can I use:
if (objectIsOfType:x && object.property == y)
or do I need to nest these? Assume that asking for object.property will through an error if the object is not of type x.
No. Objective C (as C and many other languages) uses short circuit evaluation.
Objective-C supports short-circuit evaluation(from left to right).
but in any way, you need to check object on nil :))

Upper boolean and lower boolean [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a difference between YES/No,TRUE/FALSE and true/false in objective-c?
What is the difference between upper boolean and lower boolean? Ex: true and TRUE. Thanks for any answers!
It depends on the language... Some languages only like True or False (Python), some like all lowercase (Java - I think), and some don't care (PHP, Ruby)

Is there a VB.net equivalent for C#'s ! operator?

After two years of C#, I'm now back to VB.net because of my current job. In C#, I can do a short hand testing for null or false value on a string variable like this:
if(!String.IsNullOrEmpty(blah))
{
...code goes here
}
however I'm a little confused about how to do this in VB.net.
if Not String.IsNullOrEmpty(blah) then
...code goes here
end if
Does the above statement mean if the string is not null or empty? Is the Not keyword operate like the C#'s ! operator?
In the context you show, the VB Not keyword is indeed the equivalent of the C# ! operator. But note that the VB Not keyword is actually overloaded to represent two C# equivalents:
logical negation: !
bitwise complement: ~
For example, the following two lines are equivalent:
C#: useThis &= ~doNotUse;
VB: useThis = useThis And (Not doNotUse)
Yes they are the same
Not is exactly like ! (in the context of Boolean. See RoadWarrior's remark for its semantics as one's complement in bit arithmetic). There's a special case in combination with the Is operator to test for reference equality:
If Not x Is Nothing Then ' … '
' is the same as '
If x IsNot Nothing Then ' … '
is equivalent to C#'s
if (x != null) // or, rather, to be precise:
if (object.ReferenceEquals(x, null))
Here, usage of IsNot is preferred. Unfortunately, it doesn't work for TypeOf tests.
They work identically until you reference C or C++ code.
For example doing NOT on the result of a Win32 API function, might lead to incorrect result, since true in C == 1, and a bitwise NOT on 1 does not equal false.
As 1 is 00000000001
Bitwise Not 11111111110
While false is 00000000000
However in VB it works just fine, as in VB true == -1
As -1 is 11111111111
Bitwise Not 00000000000
c# : boolean example1 = false;
boolean example2 = !example1;
vb.net: dim example1 as boolean = False
dim example2 as boolean = Not example1
Seconded. They work identically. Both reverse the logical meaning of the expression following the !/not operator.