This question already has answers here:
Casting a boolean to an integer returns -1 for true?
(11 answers)
Closed 1 year ago.
I just want to understand why the following scenario is happening:
Msgbox True
Output: True
Msgbox -True
Output: 1
Why does it become 1?
Because you force VBA to implicitly convert a boolean to a number. True is internally represented as -1, so converting it to a number will result in -1, and -(-1) is 1. False, btw, is stored as 0, so -False will print 0.
However, you should avoid to do calculations with a boolean. If you do so, it is very likely that you go a wrong path. A boolean can be true or false, that's all you need to know. Only thing you should use is Bool'sche Algebra using AND, OR and NOT
Microsoft VBA documentation about Boolean - see also What are the implicit type conversion rules in vba?
Related
This question already has answers here:
Ternary operator VB vs C#: why resolves Nothing to zero? [duplicate]
(7 answers)
Closed 3 years ago.
I was surprised when I tried to assign null value to a variable using ternary expression in vb.net. When I assign through ternary expression, it doesn't works as expected.
Dim i As Integer? = Nothing
Dim j As Integer? = Nothing
i = If(True, j, 1)
j = If(True, Nothing, 1)
After execution of this code: i is nothing but j becomes 0 (zero). Why?
What is the explanation?
Why I can"t assign directly Nothing (Null) value?
I think the important thing to understand here is Nothing in VB.Net is not the same as null in other languages, because you can still assign Nothing to value types. In many other languages, null is a reference-type construct only. If you're familiar with C#, Nothing is closer to default(T) than to null.
With that in mind, take a fresh look at this expression:
If(True, Nothing, 1)
The compiler evaluates the entire expression on it's own merits, knowing nothing about i or j, even though j is the target of the assignment. The expression has to be able to stand alone.
VB.Net must also determine a type to use for the expression, and it must do this at compile-time. It is not able to infer anything from the use of Nothing about needing an Integer? rather than a basic non-nullable Integer for this type, because VB.Net is perfectly happy to assign Nothing to value types. Therefore the type of the conditional expression can only be inferred from the 1 literal in the final argument, which is a plain Integer, and not Integer?.
Given that resulting type, we now must evaluate Nothing as an integer, where the result is the 0 you observed. In theory, this part is done at runtime rather than compile time, but in practice I suspect the compiler or jitter recognizes the chance to optimize things and rewrites it all down to just j = 0.
If you want to be able to assign an Integer? with a value of Nothing, do it as you did in the first example and keep a variable handy with the correct type you can use for the assignment.
This question already has answers here:
"Not ... Is Nothing" versus "... IsNot Nothing"
(5 answers)
Closed 5 years ago.
When we want to check not null in vb.net we use "IF Not X Is Nothing Then" but this is very vague to read and understand especially if code is complicated or large. Is there any condition with which we can check the same condition and easy to read or understand?
Use
If X IsNot Nothing Then
reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/isnot-operator
There are multiple ways to do this.
As suggested by other comments/answers you can use the IsNot operator.
Or you can use the IsNothing() function. E.g.
If Not IsNothing(someObject) Then ...
You can also use the Iif() function but I don't recommend it since it don't do short-circuiting and always evaluates all the arguments, regardless of the condition.
Probably the inline if is what you're looking for (available since VB.NET 2008 onward):
If(condition, truePart, falsePart)
This question already has answers here:
Nothing = String.Empty (Why are these equal?)
(4 answers)
Difference between C# and VB.Net string comparison
(2 answers)
Closed 7 years ago.
The code below returns TRUE when I expected it to return FALSE.
Why does it return TRUE? I Expect nothing to set the value of the string to null, not empty (According to msdn)
CodeingGround sample
Module VBModule
Sub Main()
dim x as String
x = nothing
console.writeline(x = string.Empty)
End Sub
End Module
Nothing (Visual Basic)
Represents the default value of any data type. For reference types,
the default value is the null reference.
***EDIT****
Nothing = String.Empty (Why are these equal?)
Nothing in VB.net is the default value for a type. The language spec says in section 2.4.7:
Nothing is a special literal; it does not have a type and is
convertible to all types in the type system, including type
parameters. When converted to a particular type, it is the equivalent
of the default value of that type.
So, when you test against String.Empty, Nothing is converted to a string, which has a length 0. The Is operator should be used for testing against Nothing, and String.Empty.Equals(Nothing) will also return false.
Per then comment,
when converted to a particular type, it is the equivalent of the
default value of that type.
The default value for a string is null. I dont understand why that answer was accepted.
Difference between C# and VB.Net string comparison
The above post explains the answer clearly, credit goes to Tim Schmelter in the comment section for finding the above post
Per Tim Schmeleters comments
it is called from the vb compiler as the documentation states in String.Equality Operator
Why does the following code print out its below value, specifically with regards to the CShort method in VB.Net?
Console.WriteLine(CShort(False)) 'prints 0
Console.WriteLine(CShort(True)) 'prints -1
I have been able to reproduce this with many of the Visual Basic type conversions.
Was this just a design decision in Visual Basic?
In Visual Basic, the Boolean data type is stored as a 16 bit signed integer.
In this representation , -1 evaluates to 16 one bits and 0 is, of course, 16 zero bits.
Hence, if you want the relationship True = Not False to hold, then True must have the value -1.
It's not a VB-specific thing - all VB does is implicitly cast it for you to short.
In C#, you get the same result with an explicit call to a System.Convert method - e.g., System.Convert.ToInt16(true).
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null
While this question may seem like a duplicate of many, it is actually being asked for a specific reason. Take this code, for example:
Dim n As Integer? = If(True, Nothing, 1)
In that code, the ternary expression should be returning Nothing, but it's setting n to 0. If this were C#, I could say default(int?) and it would work perfectly. Now it looks like I am going to have to ditch the ternary and use a regular If block, but I really want to use the ternary.
If Nothing were truly VB.NET's equivalent to C#'s default, how can you explain this behavior?
The VB.NET equivalent to C#'s default is the keyword Nothing. The code you wrote should compile just fine so long as Id.Value returns an Integer value.
The reason your updated sample is going wrong is because of the nature of Nothing. In VB.NET Nothing is the empty value and it's convertible to any type. Now for an If expression, the compiler has to infer what the type of the return should be, and it does this by looking at the two value arguments.
The value Nothing has no type, but the literal 1 has the type Integer. Nothing is convertible to Integer so the compiler determines Integer is the best type here. This means when Nothing is chosen as the value, it will be interpreted as Integer and not Integer?.
The easiest way to fix this is to explicitly tell the compiler that you want 1 to be treated as an Integer?.
Dim n As Integer? = If(True, Nothing, CType(1, Integer?))