When working with enums, I've noticed some behaviour that seems
completely counter-intuitive to me. I was wondering if someone here
could help restore my sanity, or at least help me to understand the
"why" of the behaviour.
After dimensioning an enum of type integer, any attribute referenced
seems to, by default, return the name of that attribute as a string,
instead of the integer value assigned to it.
The code snippet that follows demonstrates this behaviour.
Module enumtest
Private Enum Cheese As Integer
Cheddar = 1
Swiss = 2
Feta = 3
End Enum
Sub Main()
Debug.WriteLine(Cheese.Cheddar)
' "Cheddar" as System.String ...
' I expected "1" as System.Integer
'A More concrete example
Dim table As DataTable
Dim row As DataRow
Dim col As DataColumn
'Without DataType on column
table = New DataTable
col = New DataColumn
table.Columns.Add(col)
row = table.NewRow()
row.Item(0) = Cheese.Cheddar
Debug.WriteLine(row.Item(0)) ' "Cheddar" as System.String
row.Item(0) = CInt(Cheese.Cheddar)
Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
'With DateType on column
table = New DataTable
col = New DataColumn
col.DataType = GetType(System.Int32)
table.Columns.Add(col)
row = table.NewRow
row.Item(0) = Cheese.Cheddar
Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
row.Item(0) = CInt(Cheese.Cheddar)
Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
Exit Sub
End Sub
End Module
Thanks in advance for any help or insight that you can provice.
Regards,
Jason
MS DevEnv 2003 v7.1.3088
..NET Framework 1.2 v1.1.4322 SP1 6 4606
AFAIK, The debug.writeline will make a call to the ToString() method of the
object passed in. If you want to see the number try this.
Debug.WriteLine(Cint(Cheese.Cheddar))
Didn't test it but it should write out the integer value of the enum.
Chris
"Jason Larion" <ja**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand the "why" of the behaviour.
After dimensioning an enum of type integer, any attribute referenced seems to, by default, return the name of that attribute as a string, instead of the integer value assigned to it.
The code snippet that follows demonstrates this behaviour.
Module enumtest
Private Enum Cheese As Integer Cheddar = 1 Swiss = 2 Feta = 3 End Enum
Sub Main()
Debug.WriteLine(Cheese.Cheddar) ' "Cheddar" as System.String ... ' I expected "1" as System.Integer
'A More concrete example Dim table As DataTable Dim row As DataRow Dim col As DataColumn
'Without DataType on column table = New DataTable col = New DataColumn
table.Columns.Add(col)
row = table.NewRow()
row.Item(0) = Cheese.Cheddar Debug.WriteLine(row.Item(0)) ' "Cheddar" as System.String
row.Item(0) = CInt(Cheese.Cheddar) Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
'With DateType on column table = New DataTable col = New DataColumn
col.DataType = GetType(System.Int32) table.Columns.Add(col)
row = table.NewRow
row.Item(0) = Cheese.Cheddar Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
row.Item(0) = CInt(Cheese.Cheddar) Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
Exit Sub
End Sub
End Module
Thanks in advance for any help or insight that you can provice.
Regards, Jason
MS DevEnv 2003 v7.1.3088 .NET Framework 1.2 v1.1.4322 SP1 Debug.WriteLine(Cint(Cheese.Cheddar))
The only downside with that solution is that it could fail if the
enum's underlying type is (U)Long (which it isn't here but could be in
the general case). An alternative is to use
Cheese.Cheddar.ToString("d", Nothing)
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ur**************@TK2MSFTNGP15.phx.gbl... Debug.WriteLine(Cint(Cheese.Cheddar))
The only downside with that solution is that it could fail if the enum's underlying type is (U)Long (which it isn't here but could be in the general case). An alternative is to use
Cheese.Cheddar.ToString("d", Nothing)
Or just Cheese.Cheddar.ToString("d")
The second parameter belongs to the another overloaded ToString() method and
doesn't need to be included :)
Ex:
Enum Cheese As Integer
Cheddar = 1
Swiss = 2
American= 3
End Enum
Public Sub Main()
Dim myFav As Cheese = Cheese.American
Console.WriteLine(myFav.ToString("d"))
End Sub
Didn't test the above example, but I believe it's right ;)
Mythran
Chris (et al),
That's exactly the surprising behaviour... I would think that anything
that calls ToString on it would return a string-formatted object of the
_value_, not the attribute.
I.e., in the example. "1" as String. Why do I get the attribute as
string? That's the part that I'm struggling with.
Thanks,
--Jason
Chris, Master of All Things Insignificant wrote: AFAIK, The debug.writeline will make a call to the ToString() method
of the object passed in. If you want to see the number try this.
Debug.WriteLine(Cint(Cheese.Cheddar))
Didn't test it but it should write out the integer value of the enum.
Chris
"Jason Larion" <ja**********@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone
here could help restore my sanity, or at least help me to understand the "why" of the behaviour.
After dimensioning an enum of type integer, any attribute
referenced seems to, by default, return the name of that attribute as a
string, instead of the integer value assigned to it.
The code snippet that follows demonstrates this behaviour.
Module enumtest
Private Enum Cheese As Integer Cheddar = 1 Swiss = 2 Feta = 3 End Enum
Sub Main()
Debug.WriteLine(Cheese.Cheddar) ' "Cheddar" as System.String ... ' I expected "1" as System.Integer
'A More concrete example Dim table As DataTable Dim row As DataRow Dim col As DataColumn
'Without DataType on column table = New DataTable col = New DataColumn
table.Columns.Add(col)
row = table.NewRow()
row.Item(0) = Cheese.Cheddar Debug.WriteLine(row.Item(0)) ' "Cheddar" as System.String
row.Item(0) = CInt(Cheese.Cheddar) Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
'With DateType on column table = New DataTable col = New DataColumn
col.DataType = GetType(System.Int32) table.Columns.Add(col)
row = table.NewRow
row.Item(0) = Cheese.Cheddar Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
row.Item(0) = CInt(Cheese.Cheddar) Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32)
Exit Sub
End Sub
End Module
Thanks in advance for any help or insight that you can provice.
Regards, Jason
MS DevEnv 2003 v7.1.3088 .NET Framework 1.2 v1.1.4322 SP1
"Jason Larion" <ja**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... I would think that anything that calls ToString on it would return a string-formatted object of the _value_, not the attribute.
Why do I get the attribute as string? That's the part that I'm struggling with.
If you've gone to the trouble of defining an Enum, it's probably
because the "names" you've given to particular (or, more likely,
arbitrary) values are more "important" than the actual numerical
values themselves. After all, why not just use the numbers 1, 2,
3, etc. in your code? Because they're meaningless. "Cheddar",
"Swiss" and "Feta" enrich your code.
So it seems perfectly reasonable to me that calling ToString() on
an Enum value should give you the meaningful "name", rather than
some odd number "value".
Also, the "names" are more persistant. If you had, say, a Structure
containing values of your cheese Enum and saved it to a file, it
would [hopefully] save the Names, not the numerical values. Two
weeks down the line, you discover to your horror that you have to
redefine your Enum values. The "names" that you parse back out
of that file will /correctly/ map to their /new/ numerical values; if
you'd left the simple numbers in there, you'd be completely stuck.
HTH,
Phill W.
Jason,
In addition to the other comments:
The "Value" of the Enum IS the name! > Private Enum Cheese As Integer > Cheddar = 1 > Swiss = 2 > Feta = 3 > End Enum
The Cheese enum has 3 values: Cheddar, Swiss & Feta.
The fact that the Cheese.Cheddar value has a Integer representation of 1 is
an implementation detail, it could just as easily have a Byte representation
of 2 or a Long representation of
5,345,678,789,379,578 or even a Short representation of -5,789.
If you have a need for a constant Integer called Cheese with a value of 1,
then you may want to consider using a Constant instead!
Const Cheddar As Integer = 1
On a side note: IMHO Enum should support Char & String representations also,
but the designers of the framework did not have the foresight...
Hope this helps
Jay
"Jason Larion" <ja**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... Chris (et al),
That's exactly the surprising behaviour... I would think that anything that calls ToString on it would return a string-formatted object of the _value_, not the attribute.
I.e., in the example. "1" as String. Why do I get the attribute as string? That's the part that I'm struggling with.
Thanks, --Jason
Chris, Master of All Things Insignificant wrote: AFAIK, The debug.writeline will make a call to the ToString() method of the object passed in. If you want to see the number try this.
Debug.WriteLine(Cint(Cheese.Cheddar))
Didn't test it but it should write out the integer value of the enum.
Chris
"Jason Larion" <ja**********@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... > When working with enums, I've noticed some behaviour that seems > completely counter-intuitive to me. I was wondering if someone here > could help restore my sanity, or at least help me to understand the > "why" of the behaviour. > > After dimensioning an enum of type integer, any attribute referenced > seems to, by default, return the name of that attribute as a string, > instead of the integer value assigned to it. > > The code snippet that follows demonstrates this behaviour. > > Module enumtest > > Private Enum Cheese As Integer > Cheddar = 1 > Swiss = 2 > Feta = 3 > End Enum > > Sub Main() > > > Debug.WriteLine(Cheese.Cheddar) > ' "Cheddar" as System.String ... > ' I expected "1" as System.Integer > > > 'A More concrete example > Dim table As DataTable > Dim row As DataRow > Dim col As DataColumn > > > 'Without DataType on column > table = New DataTable > col = New DataColumn > > table.Columns.Add(col) > > row = table.NewRow() > > row.Item(0) = Cheese.Cheddar > Debug.WriteLine(row.Item(0)) ' "Cheddar" as System.String > > row.Item(0) = CInt(Cheese.Cheddar) > Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32) > > > 'With DateType on column > table = New DataTable > col = New DataColumn > > col.DataType = GetType(System.Int32) > table.Columns.Add(col) > > row = table.NewRow > > row.Item(0) = Cheese.Cheddar > Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32) > > row.Item(0) = CInt(Cheese.Cheddar) > Debug.WriteLine(row.Item(0)) ' "1" as System.Integer (Int32) > > Exit Sub > > End Sub > > End Module > > > Thanks in advance for any help or insight that you can provice. > > Regards, > Jason > > > MS DevEnv 2003 v7.1.3088 > .NET Framework 1.2 v1.1.4322 SP1 >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matt |
last post by:
Hi,
Recently we had some code like this cause a failure:
MyEnum myEnum = (MyEnum) (int) dt;
i.e. reading an int out of the database and casting it into a
type-safe enum.
The thought...
|
by: babylon |
last post by:
I have an enum
public enum MyEnum : int
{
X,
Y
}
I have to do
int a = (int) MyEnum.X;
can i overload the operator or other means to do something like
|
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
I have created an enum list like this:
enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
|
by: Remco |
last post by:
Hi,
Let me try to simply explain my questions.
I've created a portal site with different types of users, e.g. Portal
Administrators and Normal Users.
One base class SessionUser (has a enum...
|
by: Greg |
last post by:
Not sure if this is best place for this problem, but here it is. I have a
project that is simply a C# class that interfaces with an IFilter. This is
so I can retreive the text from Word docs. ...
| |
by: kc |
last post by:
I'm trying to pull data from a database that I have no control over the
structure of. There's a members table with a column for the member's
sex. It just stores the sex as M or F. I'd like to...
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules'...
|
by: PokerMan |
last post by:
Hi guys,
Maybe someone can explain thisi have this enum:
public enum LimitType : int
{
BottomLimit,
TopLimit,
Limit
}
|
by: mitdej |
last post by:
Hi there,
I have an several enum types that starts from a nunmber other than 0.
For example:
public enum InternalStatus
{
Pending = 1,
Ported = 2,
Suspended = 3
}
I put this values in a int...
|
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,...
|
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...
| |
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,...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |