Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nulls and Integers in VB.Net?

36 views
Skip to first unread message

DaveJG

unread,
May 20, 2003, 10:23:00 AM5/20/03
to
One of the things I was looking forward to in VB.NET was the ability
of variables to be NULL, or nothing. However, I can't seem to do
either of the following:

Dim i As Integer

i = 3
i = Nothing

if i Is Nothing Then
msgbox "Is Nothing"
End if

- or -

Dim i as Integer

i = 3
i = Null

if IsNull(i) Then
Msgbox "is null"
End If

How do I achieve this? My usage here of course is merely a test.
Imagine if I had a routine that returned the sql identity column of a
row. Appropriate values for this are any negative, zero, any positive
or Null if not found at all.

Please reply to this forum, or to da...@guYOURPANTSllett.com (remove
YOURPANTS) if you want to email me.

Shawn

unread,
May 20, 2003, 10:37:14 AM5/20/03
to
NULL is not used in .NET. System.DBNull has replaced it, but you can't
assign it to an integer
You can't use Is on an integer, but you can do this:

Dim i As Integer

i = 3
i = Nothing

if i = Nothing Then


msgbox "Is Nothing"
End if

"DaveJG" <mrda...@yahoo.com> wrote in message
news:d88a5121.03052...@posting.google.com...

Jay B. Harlow [MVP - Outlook]

unread,
May 20, 2003, 11:49:25 AM5/20/03
to
DaveJG,
In addition to what Shawn stated:

Integer is a value type, value types cannot be a database NULL. Nor can they
be Nothing where Nothing means not referencing an object.

When you assign Nothing to a value type, VB.NET assigns zeros. For
structures it 'blanks' out the structure with zeros on value members or
Nothing on reference members.

NOTE: Strings are reference types, so if you assign Nothing to a string you
get Nothing not "" or String.Empty.

Hope this helps
Jay

"DaveJG" <mrda...@yahoo.com> wrote in message
news:d88a5121.03052...@posting.google.com...

DaveJG

unread,
May 20, 2003, 3:21:16 PM5/20/03
to
Well, you _can_ do that, but it doesn't really give you the expected
results. It seems that Nothing is the same as zero.

Dim i As Integer

i = 3
i = Nothing

If i = Nothing Then
MsgBox("i is nothing")
End If
If i = 0 Then
MsgBox("i is zero")
End If

This code shows _both_ message boxes.

How are you all achieving this?

"Shawn" <bossm...@hotmail.com> wrote in message news:<eV9IQ1tH...@tk2msftngp13.phx.gbl>...

Armin Zingler

unread,
May 20, 2003, 3:30:02 PM5/20/03
to
"DaveJG" <mrda...@yahoo.com> schrieb

> Well, you _can_ do that, but it doesn't really give you the
> expected results. It seems that Nothing is the same as zero.
>
> Dim i As Integer
>
> i = 3
> i = Nothing
> If i = Nothing Then
> MsgBox("i is nothing")
> End If
> If i = 0 Then
> MsgBox("i is zero")
> End If
>
> This code shows _both_ message boxes.
>
> How are you all achieving this?

Yes, Nothing, when used with numeric data types, is zero. Apart from this,
I'd use Nothing only with reference types, so I actually don't care about
it.


--
Armin

DaveJG

unread,
May 21, 2003, 1:43:59 PM5/21/03
to
No one answered my question, but I thought I'd post this to help
someone else.

One of the things I was looking forward to in .NET was the ability to
make variables null. Well, "value types" can't be null, even though
they're objects. Consider this:

Dim MyInt as Integer
MyInt = Nothing
MsgBox(MyInt = 0) ‘ True. Nothing gets converted to a zero. Now
isn't that special?

Dim MyInt As Integer
MyInt = VariantType.Null
MsgBox(MyInt = 1) ‘ True. Perhaps this works better with Variants,
but this just gets converted to a 1.

However, this is cool:

Dim i As SqlTypes.SqlInt32
Dim myi As Integer

i = i.Null

If i.IsNull Then
MsgBox("Is null") ‘ Displays "Is Null"
Else
myi = i.Value
MsgBox(myi)
End If

i = New SqlTypes.SqlInt32(3) ‘ The only way to directly assign a
number to a SqlInt.

If i.IsNull Then
MsgBox("Is null")
Else
myi = i.Value
MsgBox(myi) ‘ Displays 3
End If

mrda...@yahoo.com (DaveJG) wrote in message news:<d88a5121.03052...@posting.google.com>...

Armin Zingler

unread,
May 21, 2003, 2:12:01 PM5/21/03
to
"DaveJG" <mrda...@yahoo.com> schrieb

> No one answered my question,

No?

> but I thought I'd post this to help
> someone else.
>
> One of the things I was looking forward to in .NET was the ability
> to make variables null. Well, "value types" can't be null, even
> though they're objects. Consider this:
>
> Dim MyInt as Integer
> MyInt = Nothing
> MsgBox(MyInt = 0) ' True. Nothing gets converted to a zero. Now
> isn't that special?


???

MyInt = Nothing is equal to MyInt = 0, that's why the msgbox shows True.
What's so special?


> Dim MyInt As Integer
> MyInt = VariantType.Null
> MsgBox(MyInt = 1) ' True. Perhaps this works better with
> Variants, but this just gets converted to a 1.

You're mixing up something. "VariantType" is an enumeration! Variants do not
exist anymore in VB.Net. Variants can have different sub types. One sub type
is Integer, one is Single, one is Null, and so on. To identify the sub type,
there's the VariantType enumeration. The values of the enumeration members
don't really matter.


> However, this is cool:
>
> Dim i As SqlTypes.SqlInt32
> Dim myi As Integer
>
> i = i.Null
>
> If i.IsNull Then
> MsgBox("Is null") ' Displays "Is Null"
> Else
> myi = i.Value
> MsgBox(myi)
> End If
>
> i = New SqlTypes.SqlInt32(3) ' The only way to directly assign
> a
> number to a SqlInt.
>
> If i.IsNull Then
> MsgBox("Is null")
> Else
> myi = i.Value
> MsgBox(myi) ' Displays 3
> End If

The type SqlInt32 is actually a structure that contains additional
information whether the value is null.

It seems I don't understand your intention. Sometimes you mention "Nothing",
sometimes "Null". Null is not Nothing. The only instance of Null is
DBNull.Value.


--
Armin

DaveJG

unread,
May 22, 2003, 1:25:40 AM5/22/03
to
My sincere apologies to all those who helped me. I wasn't thinking
when I wrote "no one answered my question"; not sure where my head
was. I appreciate both those people that took time to read my
question, and those that posted answers.

"Now isn't that special" was a sarcastic comment. I think it's
counter-intuitive that if I set an Integer to nothing that it should
evaluate to zero. I'm sure there's good reasons for it, but if a
"value type" is not capable of being Nothing then you shouldn't be
ableto set it to Nothing.

I pointed out the VariantType enum issue because I have seen code
posted that used "MyInt = VariantType.Null" as if you were setting it
to null.

What can be confusing about both of these is one can write _flawed_
code making either of the above assumptions and possibly never know
that you have a problem.

I've seen, for instance, _flawed_ code that does something like the
following:

Dim i As Integer = GetValue(Key)

If i = Nothing Then

MsgBox("Not found")
Else
.
.
End If

Private Function GetValue(Key) As Integer
Get dataset for Key
If no data
Getvalue = Nothing
End IF

End Function

Obviously this isn't real code, but it shows the problem. If in your
tests GetValue never happens to return a "real" zero, then you'll
think it's operating oppropriately. If down the road zero became an
acceptible result of GetValue(), you'd have problems.


"Armin Zingler" <az.n...@freenet.de> wrote in message news:<#9xGQU8H...@tk2msftngp13.phx.gbl>...

Armin Zingler

unread,
May 22, 2003, 6:31:56 AM5/22/03
to
"DaveJG" <mrda...@yahoo.com> schrieb

> My sincere apologies to all those who helped me. I wasn't
> thinking when I wrote "no one answered my question"; not sure where
> my head was. I appreciate both those people that took time to read
> my question, and those that posted answers.

Accepted. :-)

> "Now isn't that special" was a sarcastic comment. I think it's
> counter-intuitive that if I set an Integer to nothing that it
> should evaluate to zero. I'm sure there's good reasons for it, but
> if a "value type" is not capable of being Nothing then you shouldn't
> be ableto set it to Nothing.

I totally agree. Nothing means "no reference" to me (I mentioned it in
several posts in the past). In C# you can not set an Integer = null (Nothing
in VB.Net is equal to null in C# (but it's not DBNull)!!)

> [...]


> Obviously this isn't real code, but it shows the problem. If in
> your tests GetValue never happens to return a "real" zero, then
> you'll think it's operating oppropriately. If down the road zero
> became an acceptible result of GetValue(), you'd have problems.

I agree. Seen the code examples, I understand your confusion now. :)

--
Armin

0 new messages