473,395 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3540


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
35
by: Scott Kelley | last post by:
Is there a way to reset all elements of an array with a single instruction? I want to set all elements to zero. Currently looping to do so. thx, Scott Kelley
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
4
by: C# beginner | last post by:
namespace mynamespace { internal enum ReturnCode { Fail = -1, Success = 0, ... } } If I have to use the enumeration from a class that is in
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
3
dmjpro
by: dmjpro | last post by:
enumeration in java is nothing but a class concept. right?? an inner class can be local to a method but why not enumerations? plz help. kind regards, Dmjpro.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.