By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,248 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,248 IT Pros & Developers. It's quick & easy.

Setting Integers or Enumerations to Nothing

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a


"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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.