comparing Boolean to Nothing

# Thread: comparing Boolean to Nothing

1. Senior Member
Join Date
Dec 1969
Posts
213

## comparing Boolean to Nothing

I ran across something that I thought was strange this morning. I was trying to compare a Boolean value to Nothing. If the Boolean was True, then comparing the Boolean to Nothing returned False. However, if the Boolean was False, then comparing it to Nothing returned True.<BR><BR>To illustrate,<BR><BR>Function something(ByVal bool As Boolean)<BR>&nbsp;&nbsp;If Not (bool Is Nothing) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;If bool = True Then<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Else<BR><BR>&n bsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;End If<BR>End Function<BR><BR>So if bool was True, then the If statement evaulated to True. If bool was False, the If statement was coming back False.<BR><BR>Is this a bug, or would this be the expected behavior for .NET?

2. Senior Member
Join Date
Dec 1969
Posts
2,930

## Expected

you can&#039;t use "Is" for value types. but anyway when using &#060;&#062; Nothing<BR><BR>when you declare a bool and don&#039;t give it any value, it&#039;s defaulted to false.<BR><BR>this is checking the Boolean equivalent of not setting a value...<BR>try declaring an Int32 and not setting the value, then test it against Nothing and test it against 0. since these are value types, they always have a value (reference types don&#039;t have to have a value, and can be "Nothing")

3. Senior Member
Join Date
Dec 1969
Posts
2,930

## here try it

Module Module1<BR><BR> Sub Main()<BR> Dim i As Int32<BR> Dim b As Boolean<BR><BR> If (b &#060;&#062; Nothing) Then<BR> Console.WriteLine(b)<BR> Else<BR> Console.WriteLine(" nothing " & b)<BR> End If<BR><BR> If (b) Then<BR> Console.WriteLine(b)<BR> Else<BR> Console.WriteLine(" false " & b)<BR> End If<BR><BR> If (i &#060;&#062; Nothing) Then<BR> Console.WriteLine(i)<BR> Else<BR> Console.WriteLine(" nothing " & i)<BR> End If<BR><BR> If (i = 0) Then<BR> Console.WriteLine(" 0 " & i)<BR> Else<BR> Console.WriteLine(i)<BR> End If<BR><BR> Console.ReadLine()<BR> End Sub<BR><BR>End Module<BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,930

## In other words

it converts "Nothing" to the type you&#039;re trying to test against, if it&#039;s a value type.<BR><BR>for bool, it&#039;s false, for int, it&#039;s 0.

5. Senior Member
Join Date
Dec 1969
Posts
2,930

## oh wait a second

haha woops<BR><BR>Module Module1<BR><BR> Sub Main()<BR> Dim i As Int32<BR> Dim b As Boolean<BR><BR> If (b = Nothing) Then<BR> Console.WriteLine(" nothing " & b)<BR> Else<BR> Console.WriteLine(b)<BR> End If<BR><BR> If (b) Then<BR> Console.WriteLine(b)<BR> Else<BR> Console.WriteLine(" false " & b)<BR> End If<BR><BR> If (i = Nothing) Then<BR> Console.WriteLine(" nothing " & i)<BR> Else<BR> Console.WriteLine(i)<BR> End If<BR><BR> If (i = 0) Then<BR> Console.WriteLine(" 0 " & i)<BR> Else<BR> Console.WriteLine(i)<BR> End If<BR><BR> Console.ReadLine()<BR> End Sub<BR><BR>End Module<BR><BR><BR>that&#039;s what i meant :-P

6. Senior Member
Join Date
Dec 1969
Posts
213

## RE: In other words

Gotcha, makes sense. I guess I thought in .NET if you didn&#039;t give a variable a value, then its value was defaulted to Nothing.

7. Senior Member
Join Date
Dec 1969
Posts
2,930

## well in VB

it defines a value.<BR><BR>in C#<BR><BR>using System;<BR><BR>namespace testnothingcsharp<BR>{<BR> /// &#060;summary&#062;<BR> /// Summary description for Class1.<BR> /// &#060;/summary&#062;<BR> class Class1<BR> {<BR> /// &#060;summary&#062;<BR> /// The main entry point for the application.<BR> /// &#060;/summary&#062;<BR> [STAThread]<BR> static void Main(string[] args){<BR> Int32 i;<BR> Console.WriteLine(i);<BR> }<BR> }<BR>}<BR><BR><BR>this code will not compile because i never assign a value to i. C# is safer in this regard. a pain in the *** to VB Developers.<BR><BR>C++ gives a default value of something like -2billion, or the minimum value that an int can have. so the above code ported to c++ (as in not C++.Net) will compile and run.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•