Nothing

  • Thread starter Thread starter JohnZing
  • Start date Start date
J

JohnZing

hi
if i have IsAdmin=True or IsAdmin=False why
(IsAdmin = Nothing) is True???

i have

function test(byval IsAdmin as boolean)
If Not (IsAdmin = Nothing) Then

End if
end if

but it does not work because (IsAdmin = Nothing) is always true. Why?


thank you
 
If you say:

IsAdmin = Nothing

When you look at that variable in the debugger, you will see it is actually
False.

Booleans are value types, and therefore cannot actually be nothing. So
setting one to nothing, just makes it have its default value - which for
booleans is False.

So what happens, is that:

IsAdmin = Nothing
and
IsAdmin = False

Are equivalent in as far as the end result.
 
John
IsAdmin can never be nothing so your check doesn't make much sense.A value
type (such as a boolean) can't be null..so why even bother checking it?
Within .Net, this code is very dangerous as it won't behave the same between
languages. For example, C# won't even compile this code.

Karl
 
VB.Net will compile your
if not (IsAdming = nothing) then

to

if IsAdmin = true Then

also, you shouldn't use an = sign with nothing...but that's back to the
entire reference/value type thing.

Karl
 
So what happens, is that:

IsAdmin = Nothing
and
IsAdmin = False

got it, thank you
but,then, how can i implement this:

function test(byval IsAdmin as boolean)
If IsAdmin = Nothing Then
return "I don't Know"
elseIf IsAdmin=False then
return "Yes"
elseIf IsAdmin=True then
return "No"
End if
end if

sometimes i don't know if IsAdmin is True or False
I thought i could use
label.text=test(nothing)
 
booleans are always either true or false. So there cannot be an 'i don't
know' option. It's always either yes or no - it's never anythign else. So if
you don't set it to anything, the default is 'no'.

You could try using the SqlBoolean structure, which has a Null static member
you could set it to - which would then denote it is null (though setting it
to Nothing, wouldn't work, as this is a value type as well). However, this
isn't as widely used or intuitive as using a boolean.
 
John,
A boolean can never be nothing (except in the next version of .net ..to
confuse you ;) )...

You can use an integer when you can have three states, less than zero,
greater than zero or equal to zero which you could cleanly wrap in an
enumeration.

My guess is you are fixated on boolean == null because you are fetching a
bit field from a database that supports null? The enumeration is the best
thing to use:..there's a built-in one in VB already called Tristate never
used it...I'd probably still use my own..but that's just me.

Karl
 
if you have to support a tri-state like that, then you can't use a boolean.
You can, however do something like"

Public Enum TriState
IsUnknown
IsTrue
IsFalse
End Enum

Public Function Test (isAdmin as Tristate)
if isAdmin=Tristate.IsUnknown
....
End Function
 
Back
Top