Setting Integers or Enumerations to Nothing

D

DippyDog

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/micr...&q=can't+set+enum+to+nothing#70608745c29d048b

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

Family Tree Mike

DippyDog said:
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/micr...&q=can't+set+enum+to+nothing#70608745c29d048b

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

Tom Dacon

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
 
T

Tom Shelton

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

Herfried K. Wagner [MVP]

Tom Shelton said:
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.).
 
T

Tom Shelton

.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?
 
D

David Anton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top