booleans and Nothing

  • Thread starter Thread starter Dune
  • Start date Start date
D

Dune

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
 
Dune said:
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
 
Dune said:
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.
 
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 :)
 
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.
 
* "Dune said:
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.
 
Dune said:
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?
 
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.
 
Dune said:
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.
 
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
 
you mentioned some approaches that i hadn't even
considered before, thanks for the help, it's much
appreciated :)
 
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
 
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
 
Back
Top