Nothing

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
 
M

Marina

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.
 
K

Karl

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
 
K

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
 
J

JohnZing

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)
 
M

Marina

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.
 
K

Karl

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
 
D

David Jessee

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,684
Members
45,438
Latest member
greenleaf

Latest Threads

Top