473,769 Members | 5,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3569


"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*******@gmai l.comwrote in message
news:62******** *************** ***********@c4g 2000hsg.googleg roups.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****@communi ty.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*********@YO UKNOWTHEDRILLco mcast.netschrie b:
>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*********@YO UKNOWTHEDRILLco mcast.netschrie b:
>>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*******@gmai l.comwrote in message
news:62******** *************** ***********@c4g 2000hsg.googleg roups.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
1177
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 parameters, as well as output Events. Public Enum enmListOfValues Value1 Value2 Value3 Value4
35
5837
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
2149
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 PropertyGrids displaying these enumerations (one of which has a few hundred items in). I believe this is because the dynamically generated enumerations are not being destroyed. I have not been able to find a mechanism for deleting/destroying dynamically...
4
2564
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
5611
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 base="xs:string"> <xs:enumeration value="Apple" /> <xs:enumeration value="Orange" /> </xs:restriction>
4
3539
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. Should a single class be created that contains all of a solution's enumerations? Or should enumerations be defined directly in the files that contain the related classes? Are there any other design patterns I am overlooking?
27
2677
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 provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
77
3952
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 reStructuredText source as it is today. Please discuss it here so I can see what issues people may have.
3
1524
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
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9860
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.