473,382 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

booleans and Nothing

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
13 1590

"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
"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
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
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
* "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
"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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends,...
4
by: Dr. Know | last post by:
I seem to be having an unresolvable problem with an ASP page (darned typeless scripting engines) and JET datatypes. Don't ask why we are using JET, let's just say it has something to do with...
5
by: Christopher Benson-Manica | last post by:
I have a situation where I have many (more than 32) boolean flags: var foo=true; var bar=false; var baz=false; // etc. At various points in the script, these flags may be set or unset....
6
by: chech | last post by:
Possible to have array of booleans? Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim obj() As Object = {b1, b2, b3} dim v As Object For Each v In obj v = True Next This does not work. ...
1
by: John Dann | last post by:
This question is just for my education as there are some simple and obvious workarounds, but... I have an array of Booleans that I need to export to a text file. Currently the Boolean values are...
2
by: SiJP | last post by:
Hi, My vb.net project sends an Input object to a webservice and retrieves a Results object. The webservice is maintained by a third party, and is pretty huge. I am using the following...
14
by: cmdolcet69 | last post by:
I have setup a boolean based on the following conditions Public STRData as string Public BooleanRedFlag as boolean = False If STRData = "" then _BooleanRedFlag = TRue end If What should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.