469,658 Members | 1,872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

Setting Integers or Enumerations to Nothing

This is an old old post that I'm referencing regarding what happens
when you set an integer variable to Nothing. It gets set to zero, not
"Nothing."

http://groups.google.com/group/micro...608745c29d048b

Just to expand the understanding of the problems this behavior can
cause, because a typical enumeration (enum) is an integer, setting an
enum variable to Nothing actually sets it to it's first item...

Enum TestEnum
A
B
C
End Enum

Sub SetEnum()
Dim te as New TestEnum 'value is TestEnum.A
te = TestEnum.B 'value of te is now TestEnum.B
te = Nothing 'value of te is now back to TestEnum.A
End Sub

I pulled my hair out over this one. Fortunately (most of) it grew
back. A way around this is to set the values of the enum members
manually...

Enum TestEnum
A = 1
B = 2
C = 3
End Enum

Now setting a variable of this enum type to Nothing will still set it
to zero, but it's not longer in the defined list so a particular
select-case, for example, won't be erroneously processed. Lookout,
though, as this can be dangerous in itself. In my case, one object
raises an event passing two TestEnum values-- a current value and a
previous value. For the first event, there is no previous value, so I
set it to Nothing. But I have to be careful what I do with that
"Nothing" (actually zero) in any handler. Any thoughts on how to do
this better? Am I making this more difficult than it is? Do I need to
find a different profession?
thanks and Merry Christmas.
Dec 18 '07 #1
6 3240


"DippyDog" wrote:
This is an old old post that I'm referencing regarding what happens
when you set an integer variable to Nothing. It gets set to zero, not
"Nothing."

http://groups.google.com/group/micro...608745c29d048b

Just to expand the understanding of the problems this behavior can
cause, because a typical enumeration (enum) is an integer, setting an
enum variable to Nothing actually sets it to it's first item...

Enum TestEnum
A
B
C
End Enum

Sub SetEnum()
Dim te as New TestEnum 'value is TestEnum.A
te = TestEnum.B 'value of te is now TestEnum.B
te = Nothing 'value of te is now back to TestEnum.A
End Sub

I pulled my hair out over this one. Fortunately (most of) it grew
back. A way around this is to set the values of the enum members
manually...

Enum TestEnum
A = 1
B = 2
C = 3
End Enum

Now setting a variable of this enum type to Nothing will still set it
to zero, but it's not longer in the defined list so a particular
select-case, for example, won't be erroneously processed. Lookout,
though, as this can be dangerous in itself. In my case, one object
raises an event passing two TestEnum values-- a current value and a
previous value. For the first event, there is no previous value, so I
set it to Nothing. But I have to be careful what I do with that
"Nothing" (actually zero) in any handler. Any thoughts on how to do
this better? Am I making this more difficult than it is? Do I need to
find a different profession?
thanks and Merry Christmas.
No you don't need a different profession, I don't think...

Just don't equate enums and integers. Also, in your condition checks such
as a select,
make sure you include "Case Else". You could ignore this case, or do
something like pop
up a message. This allows you to catch conditions later when you or another
developer adds a new enum value.
Dec 18 '07 #2
You should know that integers and enumerations are what are called "value
types" in .Net, and cannot actually assume the value of Nothing. VB sort of
gives you a free pass on the assignment, by not pulling a compile error, but
it's not actually a valid assignment. Under the hood, VB just assigns it a
suitable value instead. You've already seen what choices VB makes in those
cases.

So you should adjust your coding style to explicitly assign integer or
enumeration values you want the variable to hold. A useful suggestion that
FXCop will offer you is to add an enumerated value of zero, with the
symbolic value of "None", and assign it when you want the enumeration to
hold something besides any other value. For integers, it's up to you as to
what is an invalid value (if there is such a thing for the specific variable
that you're assigning).

This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
sure which) there are nullable types that you can use instead of the bare
integers and enumerations. I'm still on 2.0, so I can't comment further.

HTH,
Tom Dacon
Dacon Software Consulting

"DippyDog" <di*******@gmail.comwrote in message
news:62**********************************@c4g2000h sg.googlegroups.com...
This is an old old post that I'm referencing regarding what happens
when you set an integer variable to Nothing. It gets set to zero, not
"Nothing."

http://groups.google.com/group/micro...608745c29d048b

Just to expand the understanding of the problems this behavior can
cause, because a typical enumeration (enum) is an integer, setting an
enum variable to Nothing actually sets it to it's first item...

Enum TestEnum
A
B
C
End Enum

Sub SetEnum()
Dim te as New TestEnum 'value is TestEnum.A
te = TestEnum.B 'value of te is now TestEnum.B
te = Nothing 'value of te is now back to TestEnum.A
End Sub

I pulled my hair out over this one. Fortunately (most of) it grew
back. A way around this is to set the values of the enum members
manually...

Enum TestEnum
A = 1
B = 2
C = 3
End Enum

Now setting a variable of this enum type to Nothing will still set it
to zero, but it's not longer in the defined list so a particular
select-case, for example, won't be erroneously processed. Lookout,
though, as this can be dangerous in itself. In my case, one object
raises an event passing two TestEnum values-- a current value and a
previous value. For the first event, there is no previous value, so I
set it to Nothing. But I have to be careful what I do with that
"Nothing" (actually zero) in any handler. Any thoughts on how to do
this better? Am I making this more difficult than it is? Do I need to
find a different profession?
thanks and Merry Christmas.

Dec 18 '07 #3
On 2007-12-18, Tom Dacon <td****@community.nospamwrote:
You should know that integers and enumerations are what are called "value
types" in .Net, and cannot actually assume the value of Nothing. VB sort of
gives you a free pass on the assignment, by not pulling a compile error, but
it's not actually a valid assignment. Under the hood, VB just assigns it a
suitable value instead. You've already seen what choices VB makes in those
cases.

So you should adjust your coding style to explicitly assign integer or
enumeration values you want the variable to hold. A useful suggestion that
FXCop will offer you is to add an enumerated value of zero, with the
symbolic value of "None", and assign it when you want the enumeration to
hold something besides any other value. For integers, it's up to you as to
what is an invalid value (if there is such a thing for the specific variable
that you're assigning).

This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
sure which) there are nullable types that you can use instead of the bare
integers and enumerations. I'm still on 2.0, so I can't comment further.
VB.NET 2005 has nullable types...

--
Tom Shelton
Dec 18 '07 #4
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netschrieb:
>You should know that integers and enumerations are what are called "value
types" in .Net, and cannot actually assume the value of Nothing. VB sort
of
gives you a free pass on the assignment, by not pulling a compile error,
but
it's not actually a valid assignment. Under the hood, VB just assigns it
a
suitable value instead. You've already seen what choices VB makes in
those
cases.

So you should adjust your coding style to explicitly assign integer or
enumeration values you want the variable to hold. A useful suggestion
that
FXCop will offer you is to add an enumerated value of zero, with the
symbolic value of "None", and assign it when you want the enumeration to
hold something besides any other value. For integers, it's up to you as
to
what is an invalid value (if there is such a thing for the specific
variable
that you're assigning).

This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm
not
sure which) there are nullable types that you can use instead of the bare
integers and enumerations. I'm still on 2.0, so I can't comment further.

VB.NET 2005 has nullable types...
..NET Framework 2.0 has nullable types, which are simply implemented as a
generic class 'Nullable(Of T)'. VB 2008 will provide the syntactical sugar
which makes using nullable types easier ('Date?' instead of 'Nullable(Of
Date)' etc.).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 18 '07 #5
On 2007-12-18, Herfried K. Wagner [MVP] <hi***************@gmx.atwrote:
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netschrieb:
>>You should know that integers and enumerations are what are called "value
types" in .Net, and cannot actually assume the value of Nothing. VB sort
of
gives you a free pass on the assignment, by not pulling a compile error,
but
it's not actually a valid assignment. Under the hood, VB just assigns it
a
suitable value instead. You've already seen what choices VB makes in
those
cases.

So you should adjust your coding style to explicitly assign integer or
enumeration values you want the variable to hold. A useful suggestion
that
FXCop will offer you is to add an enumerated value of zero, with the
symbolic value of "None", and assign it when you want the enumeration to
hold something besides any other value. For integers, it's up to you as
to
what is an invalid value (if there is such a thing for the specific
variable
that you're assigning).

This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm
not
sure which) there are nullable types that you can use instead of the bare
integers and enumerations. I'm still on 2.0, so I can't comment further.

VB.NET 2005 has nullable types...

.NET Framework 2.0 has nullable types, which are simply implemented as a
generic class 'Nullable(Of T)'. VB 2008 will provide the syntactical sugar
which makes using nullable types easier ('Date?' instead of 'Nullable(Of
Date)' etc.).
Right - but, you can still make use of them in 2005. To be honest, I
haven't been playing with VB2008 yet... So, do you also get the ??
operator as well?

--
Tom Shelton
Dec 18 '07 #6
You're confusing 'Nothing' with 'null' in C#.
'Nothing' in VB is more general than 'null' in C# and means 'the default
value' in the context.
You can assign and compare value types in VB to 'Nothing' (when comparing
you use '=' instead of 'Is' with value types).

It gets more confusing with strings where "= Nothing" and "Is Nothing" both
work and produce sometimes different results, but that's another topic...
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
C++ to Ruby
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB or C# to C++/CLI
"Tom Dacon" wrote:
You should know that integers and enumerations are what are called "value
types" in .Net, and cannot actually assume the value of Nothing. VB sort of
gives you a free pass on the assignment, by not pulling a compile error, but
it's not actually a valid assignment. Under the hood, VB just assigns it a
suitable value instead. You've already seen what choices VB makes in those
cases.

So you should adjust your coding style to explicitly assign integer or
enumeration values you want the variable to hold. A useful suggestion that
FXCop will offer you is to add an enumerated value of zero, with the
symbolic value of "None", and assign it when you want the enumeration to
hold something besides any other value. For integers, it's up to you as to
what is an invalid value (if there is such a thing for the specific variable
that you're assigning).

This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
sure which) there are nullable types that you can use instead of the bare
integers and enumerations. I'm still on 2.0, so I can't comment further.

HTH,
Tom Dacon
Dacon Software Consulting

"DippyDog" <di*******@gmail.comwrote in message
news:62**********************************@c4g2000h sg.googlegroups.com...
This is an old old post that I'm referencing regarding what happens
when you set an integer variable to Nothing. It gets set to zero, not
"Nothing."

http://groups.google.com/group/micro...608745c29d048b

Just to expand the understanding of the problems this behavior can
cause, because a typical enumeration (enum) is an integer, setting an
enum variable to Nothing actually sets it to it's first item...

Enum TestEnum
A
B
C
End Enum

Sub SetEnum()
Dim te as New TestEnum 'value is TestEnum.A
te = TestEnum.B 'value of te is now TestEnum.B
te = Nothing 'value of te is now back to TestEnum.A
End Sub

I pulled my hair out over this one. Fortunately (most of) it grew
back. A way around this is to set the values of the enum members
manually...

Enum TestEnum
A = 1
B = 2
C = 3
End Enum

Now setting a variable of this enum type to Nothing will still set it
to zero, but it's not longer in the defined list so a particular
select-case, for example, won't be erroneously processed. Lookout,
though, as this can be dangerous in itself. In my case, one object
raises an event passing two TestEnum values-- a current value and a
previous value. For the first event, there is no previous value, so I
set it to Nothing. But I have to be careful what I do with that
"Nothing" (actually zero) in any handler. Any thoughts on how to do
this better? Am I making this more difficult than it is? Do I need to
find a different profession?
thanks and Merry Christmas.


Dec 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Plinkerton | last post: by
35 posts views Thread by Scott Kelley | last post: by
1 post views Thread by someone else | last post: by
4 posts views Thread by C# beginner | last post: by
1 post views Thread by Oleg Ogurok | last post: by
4 posts views Thread by ChrisB | last post: by
77 posts views Thread by Ben Finney | last post: by
dmjpro
3 posts views Thread by dmjpro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.