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