By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

booleans and Nothing

P: n/a
How do I compare a boolean with Nothing?

I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.

Cheers
Nov 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

"Dune" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
How do I compare a boolean with Nothing?

I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.

Cheers


I may be wrong, but I don't think a Boolean can ever be nothing.. I think
it's initialized as FALSE, but you can very easily check that, make a form
or app, make a boolean, and step through the code, watching the locals
window and see what that sucker get's inited as. My solution, in the
initializer, eg Sub Main, or FormLoad, I explicitly set that sucker and deal
with it that way, I don't like being unsure.
HTH
Sueffel
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004
Nov 20 '05 #2

P: n/a
"Dune" <an*******@discussions.microsoft.com> schrieb
How do I compare a boolean with Nothing?
Not at all.
I can't say "If boolVar Is Nothing" because a boolean is
not a reference type,
right
but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.


No:
false = nothing => true
true = nothing => false
Don't use Nothing with value types. It doesn't make sense. Unfortunatelly it
is possible. Nothing used with value types is equal to the default value of
the value type, this is False for the Boolean type.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
hmmm, yes, you're right. A boolean is always initialised
to false.

this is kinda a problem for me...i wanted to check for
Nothing so i can control the parameters for a stored
procedure...so if boolVar is Nothing, i will pass in
DBNull.Value as the parameter and if boolVar is NOT
Nothing, i will pass in the value of boolVar. Oh well,
guess i'll have to find a different approach.

thanks for the quick reply :)
-----Original Message-----

"Dune" <an*******@discussions.microsoft.com> wrote in messagenews:04****************************@phx.gbl...
How do I compare a boolean with Nothing?

I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.

Cheers
I may be wrong, but I don't think a Boolean can ever be

nothing.. I thinkit's initialized as FALSE, but you can very easily check that, make a formor app, make a boolean, and step through the code, watching the localswindow and see what that sucker get's inited as. My solution, in theinitializer, eg Sub Main, or FormLoad, I explicitly set that sucker and dealwith it that way, I don't like being unsure.
HTH
Sueffel
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004

.

Nov 20 '05 #4

P: n/a
Thanks for the clarification and the quick reply :)

i was hoping to compare to Nothing in order to control the
parameters for a stored procedure...so if boolvar is
Nothing, i would pass in DBNull.Value instead. I guess
I'll have to find some other way of going about it.
-----Original Message-----
"Dune" <an*******@discussions.microsoft.com> schrieb
How do I compare a boolean with Nothing?
Not at all.
I can't say "If boolVar Is Nothing" because a boolean is
not a reference type,


right
but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.


No:
false = nothing => true
true = nothing => false
Don't use Nothing with value types. It doesn't make

sense. Unfortunatelly itis possible. Nothing used with value types is equal to the default value ofthe value type, this is False for the Boolean type.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #5

P: n/a
* "Dune" <an*******@discussions.microsoft.com> scripsit:
How do I compare a boolean with Nothing?

I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.


Boolean is a value type with values 'True'/'False'. It cannot store
'Nothing'. You may want to write a new class which stores the Boolean
value and provides functionality to indicate if the value is 'Nothing'
or not. In .NET 2.0 there will be a generic 'System.Nullable' class
which will make dealing with "null" values easier.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
"Dune" <an*******@discussions.microsoft.com> schrieb
Thanks for the clarification and the quick reply :)

i was hoping to compare to Nothing in order to control the
parameters for a stored procedure...so if boolvar is
Nothing, i would pass in DBNull.Value instead. I guess
I'll have to find some other way of going about it.


Can't you set the Parameter's Value property to DBNull.Value?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7

P: n/a
Cor
Hi Dune,

Just an advice, do not mix up database items with memory values.

Cor
hmmm, yes, you're right. A boolean is always initialised
to false.

this is kinda a problem for me...i wanted to check for
Nothing so i can control the parameters for a stored
procedure...so if boolVar is Nothing, i will pass in
DBNull.Value as the parameter and if boolVar is NOT
Nothing, i will pass in the value of boolVar. Oh well,
guess i'll have to find a different approach.

thanks for the quick reply :)

Nov 20 '05 #8

P: n/a
Sorry, I wasn't very clear...yes, i am setting the
Parameter's Value property to DBNull.Value.

The problem is trying to figure out what to set it to.

I want to set the Parameter's Value property to
DBNull.Value when a given variable is Nothing. If that
variable is not Nothing, I want to set the Parameter's
Value property to equal that variable, like so:

Public Sub UpdateUser(ByVal userId As Integer, ByVal
active as Boolean, ByVal Name as String, ...)

... ' set up command objects etc...

Dim arParams() As SqlParameter = New SqlParameter(7) {}

arParams(0) = New SqlParameter("@UserId", userId)

If active = Nothing Then
arParams(1) = New SqlParameter("@Active", DBNull.Value)
Else
arParams(1) = New SqlParameter("@Active", active)
End If

arParams(2) = New SqlParameter("@Name", name)

... ' other parameters

... ' execute the stored procedure

End Sub

So, in my stored procedure itself, I will check for NULL
before updating.

This means that whoever calls the UpdateUser method has a
choice of whether they want to update the Active field or
not. If they don't want to update it, they can just pass
in Nothing to the method. However, because I cannot
compare Nothing to a Boolean, the code above will not work.
-----Original Message-----
"Dune" <an*******@discussions.microsoft.com> schrieb
Thanks for the clarification and the quick reply :)

i was hoping to compare to Nothing in order to control the parameters for a stored procedure...so if boolvar is
Nothing, i would pass in DBNull.Value instead. I guess
I'll have to find some other way of going about it.
Can't you set the Parameter's Value property to

DBNull.Value?

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #9

P: n/a
Dune.... Bools are Value Types and won't ever be nothing. Perhaps create an
enum which has true, false, undefined (or nothing) and check for each of
those respectively.

HTH,

Bill
"Dune" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
How do I compare a boolean with Nothing?

I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.

Cheers

Nov 20 '05 #10

P: n/a
"Dune" <an*******@discussions.microsoft.com> schrieb
Public Sub UpdateUser(ByVal userId As Integer, ByVal
active as Boolean, ByVal Name as String, ...) If active = Nothing Then So, in my stored procedure itself, I will check for NULL
before updating.

This means that whoever calls the UpdateUser method has a
choice of whether they want to update the Active field or
not. If they don't want to update it, they can just pass
in Nothing to the method. However, because I cannot
compare Nothing to a Boolean, the code above will not work.


As Booleans can not store Nothing, you could write an overloaded version:

Public Sub UpdateUser(ByVal userId As Integer, ByVal Name as String, ...)
If you've got a lot of arguments, you might consider other approaches, too.
For example, you could write one procedure and a "Flags" argument that
determines which fields are relevant:

<flags()> _
Enum FieldsToUpdate
Active
Name
'...
end enum
Public Sub UpdateUser(ByVal userId As Integer, byval Flags as
FieldsToUpdate, ByVal Name as String, ...)

if (flags and FieldsToUpdate.active)=active then
'...
end if
if (flags and FieldsToUpdate.name) = name then
'...
end if

end sub

Another sub making this easier would help, so you'd only have to write one
line per field.

well, you could also declare the arguments As Object, so you'll be able
to pass Nothing, but you'll loose type safety which should usually be
preferred over simplification.
Depending on the current (and future) structure of your application and the
level of database integration, there, of course, can also better approaches.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11

P: n/a
you mentioned some approaches that i hadn't even
considered before, thanks for the help, it's much
appreciated :)
-----Original Message-----
"Dune" <an*******@discussions.microsoft.com> schrieb
Public Sub UpdateUser(ByVal userId As Integer, ByVal
active as Boolean, ByVal Name as String, ...)
If active = Nothing Then

So, in my stored procedure itself, I will check for NULL
before updating.

This means that whoever calls the UpdateUser method has a choice of whether they want to update the Active field or not. If they don't want to update it, they can just pass
in Nothing to the method. However, because I cannot
compare Nothing to a Boolean, the code above will not

work.
As Booleans can not store Nothing, you could write an overloaded version:
Public Sub UpdateUser(ByVal userId As Integer, ByVal Name as String, ...)

If you've got a lot of arguments, you might consider other approaches, too.For example, you could write one procedure and a "Flags" argument thatdetermines which fields are relevant:

<flags()> _
Enum FieldsToUpdate
Active
Name
'...
end enum
Public Sub UpdateUser(ByVal userId As Integer, byval Flags asFieldsToUpdate, ByVal Name as String, ...)

if (flags and FieldsToUpdate.active)=active then
'...
end if
if (flags and FieldsToUpdate.name) = name then
'...
end if

end sub

Another sub making this easier would help, so you'd only have to write oneline per field.

well, you could also declare the arguments As Object, so you'll be ableto pass Nothing, but you'll loose type safety which should usually bepreferred over simplification.
Depending on the current (and future) structure of your application and thelevel of database integration, there, of course, can also better approaches.

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #12

P: n/a
Dune,
Consider using the structures in System.Data.SqlTypes namespace for your
parameters. They support the concept of a Database Null.

Of course they are more work to use in VB.NET as VB.NET does not yet support
Operator Overloading.

As Herfried suggested, this will get easier in Whidbey, as it has a
Nullable/Optional type, as well as VB.NET will support Operator Overloading,
which means that you can use SqlTypes & regular types interchangably...

Hope this helps
Jay

"Dune" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
Thanks for the clarification and the quick reply :)

i was hoping to compare to Nothing in order to control the
parameters for a stored procedure...so if boolvar is
Nothing, i would pass in DBNull.Value instead. I guess
I'll have to find some other way of going about it.
-----Original Message-----
"Dune" <an*******@discussions.microsoft.com> schrieb
How do I compare a boolean with Nothing?


Not at all.
I can't say "If boolVar Is Nothing" because a boolean is
not a reference type,


right
but "If boolVar = Nothing" always
seems to return true, even if the boolean has a value of
true or false.


No:
false = nothing => true
true = nothing => false
Don't use Nothing with value types. It doesn't make

sense. Unfortunatelly it
is possible. Nothing used with value types is equal to

the default value of
the value type, this is False for the Boolean type.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #13

P: n/a
I was vaguely aware of the existence SqlTypes but it never
really clicked that they could be used this way...after
some experimentation, i can see that they really are
rather nifty :) thanks heaps for the suggestion
-----Original Message-----
Dune,
Consider using the structures in System.Data.SqlTypes namespace for yourparameters. They support the concept of a Database Null.

Of course they are more work to use in VB.NET as VB.NET does not yet supportOperator Overloading.

As Herfried suggested, this will get easier in Whidbey, as it has aNullable/Optional type, as well as VB.NET will support Operator Overloading,which means that you can use SqlTypes & regular types interchangably...
Hope this helps
Jay

"Dune" <an*******@discussions.microsoft.com> wrote in messagenews:05****************************@phx.gbl...
Thanks for the clarification and the quick reply :)

i was hoping to compare to Nothing in order to control the parameters for a stored procedure...so if boolvar is
Nothing, i would pass in DBNull.Value instead. I guess
I'll have to find some other way of going about it.
>-----Original Message-----
>"Dune" <an*******@discussions.microsoft.com> schrieb
>> How do I compare a boolean with Nothing?
>
>Not at all.
>
>> I can't say "If boolVar Is Nothing" because a boolean is >> not a reference type,
>
>right
>
>> but "If boolVar = Nothing" always
>> seems to return true, even if the boolean has a value of >> true or false.
>
>No:
>false = nothing => true
>true = nothing => false
>
>
>Don't use Nothing with value types. It doesn't make

sense. Unfortunatelly it
>is possible. Nothing used with value types is equal to

the default value of
>the value type, this is False for the Boolean type.
>
>
>--
>Armin
>
>http://www.plig.net/nnq/nquote.html
>http://www.netmeister.org/news/learn2quote.html
>
>.
>

.

Nov 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.