473,756 Members | 7,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ENUM is string or integer ??

Hello,

In VS2003, I tried using an enum and setting it into a field in a
datarow. It seems as if the datatype of the field in the row
determined what went into the field. If the datatype was string, then
the name of the enum item went into the field, but if the datatype was
integer, then the integer value of the enum was stored.

Is this expected behaviour? I couldn't find anything while searching
for answers.

TIA
Barry

'Here is a sample to demonstrate
'option strict and option explicit are on in all files

Public Class testclass2
Public Enum TESTENUM
AA = 1
BB = 2
End Enum
'And other code
End Class

'this is called in a separate form

Private Sub showthis()
Dim dd As System.Data.Dat aRow
Dim cc As System.Data.Dat aColumn
Dim tt As System.Data.Dat aTable

tt = New System.Data.Dat aTable

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.St ring")
.ColumnName = "string"
End With
tt.Columns.Add( cc)

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.In t32")
.ColumnName = "integer"
End With
tt.Columns.Add( cc)

Dim aa() As Object
ReDim aa(1)
aa(0) = "ss"
aa(1) = 100
tt.Rows.Add(aa)

With tt
.Rows(0).Item(" string") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("string") .ToString)
.Rows(0).Item(" integer") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("integer" ).ToString)
End With

End Sub

bceggersATcomca stDOTnet
Dec 24 '05 #1
5 2873
Hi,

Yes it is. The enums tostring will return the name of the enum.
Ken
----------------
"Barry" <bc******@castc om.com> wrote in message
news:2m******** *************** *********@4ax.c om...
Hello,

In VS2003, I tried using an enum and setting it into a field in a
datarow. It seems as if the datatype of the field in the row
determined what went into the field. If the datatype was string, then
the name of the enum item went into the field, but if the datatype was
integer, then the integer value of the enum was stored.

Is this expected behaviour? I couldn't find anything while searching
for answers.

TIA
Barry

'Here is a sample to demonstrate
'option strict and option explicit are on in all files

Public Class testclass2
Public Enum TESTENUM
AA = 1
BB = 2
End Enum
'And other code
End Class

'this is called in a separate form

Private Sub showthis()
Dim dd As System.Data.Dat aRow
Dim cc As System.Data.Dat aColumn
Dim tt As System.Data.Dat aTable

tt = New System.Data.Dat aTable

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.St ring")
.ColumnName = "string"
End With
tt.Columns.Add( cc)

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.In t32")
.ColumnName = "integer"
End With
tt.Columns.Add( cc)

Dim aa() As Object
ReDim aa(1)
aa(0) = "ss"
aa(1) = 100
tt.Rows.Add(aa)

With tt
.Rows(0).Item(" string") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("string") .ToString)
.Rows(0).Item(" integer") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("integer" ).ToString)
End With

End Sub

bceggersATcomca stDOTnet

Dec 24 '05 #2
This is because of an implicit cast ?

--
Best Regards

The Inimitable Mr Newbie º¿º

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:uy******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Yes it is. The enums tostring will return the name of the enum.
Ken
----------------
"Barry" <bc******@castc om.com> wrote in message
news:2m******** *************** *********@4ax.c om...
Hello,

In VS2003, I tried using an enum and setting it into a field in a
datarow. It seems as if the datatype of the field in the row
determined what went into the field. If the datatype was string, then
the name of the enum item went into the field, but if the datatype was
integer, then the integer value of the enum was stored.

Is this expected behaviour? I couldn't find anything while searching
for answers.

TIA
Barry

'Here is a sample to demonstrate
'option strict and option explicit are on in all files

Public Class testclass2
Public Enum TESTENUM
AA = 1
BB = 2
End Enum
'And other code
End Class

'this is called in a separate form

Private Sub showthis()
Dim dd As System.Data.Dat aRow
Dim cc As System.Data.Dat aColumn
Dim tt As System.Data.Dat aTable

tt = New System.Data.Dat aTable

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.St ring")
.ColumnName = "string"
End With
tt.Columns.Add( cc)

cc = New System.Data.Dat aColumn
With cc
.AllowDBNull = False
.DataType = System.Type.Get Type("System.In t32")
.ColumnName = "integer"
End With
tt.Columns.Add( cc)

Dim aa() As Object
ReDim aa(1)
aa(0) = "ss"
aa(1) = 100
tt.Rows.Add(aa)

With tt
.Rows(0).Item(" string") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("string") .ToString)
.Rows(0).Item(" integer") = testclass2.TEST ENUM.AA

System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("integer" ).ToString)
End With

End Sub

bceggersATcomca stDOTnet


Dec 24 '05 #3
Mr Newbie,
I don't think its an implicit cast as much as an explicit convert in the
DataRow.Item property.

I strongly suspect that DataRow.Item is using Convert.ChangeT ype under the
covers! Convert.ChangeT ype can convert an Enum to an Integer or a String
based on the desired type...

Dim value1 As Object = Convert.ChangeT ype(TESTENUM.AA ,
GetType(String) )
Dim value2 As Object = Convert.ChangeT ype(TESTENUM.AA ,
GetType(Integer ))
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Mr Newbie" <he**@now.com > wrote in message
news:OL******** ******@tk2msftn gp13.phx.gbl...
| This is because of an implicit cast ?
|
| --
| Best Regards
|
| The Inimitable Mr Newbie º¿º
|
|
|
| "Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
| news:uy******** ******@TK2MSFTN GP12.phx.gbl...
| > Hi,
| >
| > Yes it is. The enums tostring will return the name of the enum.
| >
| >
| > Ken
| > ----------------
| > "Barry" <bc******@castc om.com> wrote in message
| > news:2m******** *************** *********@4ax.c om...
| >> Hello,
| >>
| >> In VS2003, I tried using an enum and setting it into a field in a
| >> datarow. It seems as if the datatype of the field in the row
| >> determined what went into the field. If the datatype was string, then
| >> the name of the enum item went into the field, but if the datatype was
| >> integer, then the integer value of the enum was stored.
| >>
| >> Is this expected behaviour? I couldn't find anything while searching
| >> for answers.
| >>
| >> TIA
| >> Barry
| >>
| >> 'Here is a sample to demonstrate
| >> 'option strict and option explicit are on in all files
| >>
| >> Public Class testclass2
| >> Public Enum TESTENUM
| >> AA = 1
| >> BB = 2
| >> End Enum
| >> 'And other code
| >> End Class
| >>
| >> 'this is called in a separate form
| >>
| >> Private Sub showthis()
| >> Dim dd As System.Data.Dat aRow
| >> Dim cc As System.Data.Dat aColumn
| >> Dim tt As System.Data.Dat aTable
| >>
| >> tt = New System.Data.Dat aTable
| >>
| >> cc = New System.Data.Dat aColumn
| >> With cc
| >> .AllowDBNull = False
| >> .DataType = System.Type.Get Type("System.St ring")
| >> .ColumnName = "string"
| >> End With
| >> tt.Columns.Add( cc)
| >>
| >> cc = New System.Data.Dat aColumn
| >> With cc
| >> .AllowDBNull = False
| >> .DataType = System.Type.Get Type("System.In t32")
| >> .ColumnName = "integer"
| >> End With
| >> tt.Columns.Add( cc)
| >>
| >> Dim aa() As Object
| >> ReDim aa(1)
| >> aa(0) = "ss"
| >> aa(1) = 100
| >> tt.Rows.Add(aa)
| >>
| >> With tt
| >> .Rows(0).Item(" string") = testclass2.TEST ENUM.AA
| >>
| >> System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("string") .ToString)
| >> .Rows(0).Item(" integer") = testclass2.TEST ENUM.AA
| >>
| >> System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("integer" ).ToString)
| >> End With
| >>
| >> End Sub
| >>
| >>
| >>
| >> bceggersATcomca stDOTnet
| >
| >
|
|
Dec 24 '05 #4
Barry,
In addition to the other comments.

DataRow will convert the input value into the respective
DataColumn.Data Type. As I told Mr Newbiew, I strongly suspect that DataRow
is using Convert.ChangeT ype to do this conversion.

Effectively:

Public Class DataRow
'...
Dim m_columns() As DataColumn
Dim m_values() As Object

Public Property Item(ByVal index As Integer) As Object
Get
Return m_values(index)
End Get
Set(ByVal value As Object)
m_values(index) = Convert.ChangeT ype(value,
m_columns(index ).datatype)
End Set
End Property
End Class
An enum can easily be converted into either an Integer (its value) or a
String (its name).
FWIW: Rather then use Type.GetType to set the DataType of a DataColumn, I
would recommend the GetType keyword.

| .DataType = System.Type.Get Type("System.St ring")
.DataType = GetType(String)
| .DataType = System.Type.Get Type("System.In t32")
.DataType = GetType(Integer )

Using the GetType keyword avoids a runtime error if you mistype the type
name. I use Type.GetType where I am reading the types from an external
source, such as a file...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Barry" <bc******@castc om.com> wrote in message
news:2m******** *************** *********@4ax.c om...
| Hello,
|
| In VS2003, I tried using an enum and setting it into a field in a
| datarow. It seems as if the datatype of the field in the row
| determined what went into the field. If the datatype was string, then
| the name of the enum item went into the field, but if the datatype was
| integer, then the integer value of the enum was stored.
|
| Is this expected behaviour? I couldn't find anything while searching
| for answers.
|
| TIA
| Barry
|
| 'Here is a sample to demonstrate
| 'option strict and option explicit are on in all files
|
| Public Class testclass2
| Public Enum TESTENUM
| AA = 1
| BB = 2
| End Enum
| 'And other code
| End Class
|
| 'this is called in a separate form
|
| Private Sub showthis()
| Dim dd As System.Data.Dat aRow
| Dim cc As System.Data.Dat aColumn
| Dim tt As System.Data.Dat aTable
|
| tt = New System.Data.Dat aTable
|
| cc = New System.Data.Dat aColumn
| With cc
| .AllowDBNull = False
| .DataType = System.Type.Get Type("System.St ring")
| .ColumnName = "string"
| End With
| tt.Columns.Add( cc)
|
| cc = New System.Data.Dat aColumn
| With cc
| .AllowDBNull = False
| .DataType = System.Type.Get Type("System.In t32")
| .ColumnName = "integer"
| End With
| tt.Columns.Add( cc)
|
| Dim aa() As Object
| ReDim aa(1)
| aa(0) = "ss"
| aa(1) = 100
| tt.Rows.Add(aa)
|
| With tt
| .Rows(0).Item(" string") = testclass2.TEST ENUM.AA
|
| System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("string") .ToString)
| .Rows(0).Item(" integer") = testclass2.TEST ENUM.AA
|
| System.Windows. Forms.MessageBo x.Show(.Rows(0) .Item("integer" ).ToString)
| End With
|
| End Sub
|
|
|
| bceggersATcomca stDOTnet
Dec 24 '05 #5
Thanks for the tip....

And thanks to all for the answers !!!
Merry Christmas !!

On Sat, 24 Dec 2005 11:47:24 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************ @tsbradley.net> wrote:
Barry,
In addition to the other comments.

DataRow will convert the input value into the respective
DataColumn.Dat aType. As I told Mr Newbiew, I strongly suspect that DataRow
is using Convert.ChangeT ype to do this conversion.

Effectively:

Public Class DataRow
'...
Dim m_columns() As DataColumn
Dim m_values() As Object

Public Property Item(ByVal index As Integer) As Object
Get
Return m_values(index)
End Get
Set(ByVal value As Object)
m_values(index) = Convert.ChangeT ype(value,
m_columns(inde x).datatype)
End Set
End Property
End Class
An enum can easily be converted into either an Integer (its value) or a
String (its name).
FWIW: Rather then use Type.GetType to set the DataType of a DataColumn, I
would recommend the GetType keyword.

| .DataType = System.Type.Get Type("System.St ring")
.DataType = GetType(String)
| .DataType = System.Type.Get Type("System.In t32")
.DataType = GetType(Integer )

Using the GetType keyword avoids a runtime error if you mistype the type
name. I use Type.GetType where I am reading the types from an external
source, such as a file...


bceggersATcomca stDOTnet
Dec 24 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4293
by: Voronkov Konstantin | last post by:
Thank you for answer, but I still did not got *how* to make serialization of enum type. Can you provide more instructions or hint, please? My task is to serialize enum to something like byte array, and then load enum from that byte array to the same enum type on other platform. One way to do I see is:
3
16077
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
1
1941
by: arzewski | last post by:
I don't know what was the reason from a language design point-of-view to allow this, but VB.NET compiler does not flag you (when C# compiler does) when passing an integer value as a parameter to a function that, from its signature, it is expecting an enumerated type. In the code below, invoking SetGender(2) will set a female. What if later on the order of the enum types declared are changed such that invoking the same code would then...
2
3399
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function and return the name "FirstData"
6
4618
by: Jason Larion | last post by:
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...
13
12392
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
6
34963
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do something similar with an Enum created in Access? Alright, here is some air code to explain what I mean Public Enum MyEnum
3
5234
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
11
6407
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently we follow the path below: intValue --enum name --enum value
0
9275
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
10040
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
9873
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...
0
9713
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
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3806
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
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.