473,382 Members | 1,353 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,382 software developers and data experts.

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.DataRow
Dim cc As System.Data.DataColumn
Dim tt As System.Data.DataTable

tt = New System.Data.DataTable

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.String")
.ColumnName = "string"
End With
tt.Columns.Add(cc)

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.Int32")
.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.TESTENUM.AA

System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("string").ToString)
.Rows(0).Item("integer") = testclass2.TESTENUM.AA

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

End Sub

bceggersATcomcastDOTnet
Dec 24 '05 #1
5 2854
Hi,

Yes it is. The enums tostring will return the name of the enum.
Ken
----------------
"Barry" <bc******@castcom.com> wrote in message
news:2m********************************@4ax.com...
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.DataRow
Dim cc As System.Data.DataColumn
Dim tt As System.Data.DataTable

tt = New System.Data.DataTable

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.String")
.ColumnName = "string"
End With
tt.Columns.Add(cc)

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.Int32")
.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.TESTENUM.AA

System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("string").ToString)
.Rows(0).Item("integer") = testclass2.TESTENUM.AA

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

End Sub

bceggersATcomcastDOTnet

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

--
Best Regards

The Inimitable Mr Newbie º¿º

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
Hi,

Yes it is. The enums tostring will return the name of the enum.
Ken
----------------
"Barry" <bc******@castcom.com> wrote in message
news:2m********************************@4ax.com...
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.DataRow
Dim cc As System.Data.DataColumn
Dim tt As System.Data.DataTable

tt = New System.Data.DataTable

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.String")
.ColumnName = "string"
End With
tt.Columns.Add(cc)

cc = New System.Data.DataColumn
With cc
.AllowDBNull = False
.DataType = System.Type.GetType("System.Int32")
.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.TESTENUM.AA

System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("string").ToString)
.Rows(0).Item("integer") = testclass2.TESTENUM.AA

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

End Sub

bceggersATcomcastDOTnet


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.ChangeType under the
covers! Convert.ChangeType can convert an Enum to an Integer or a String
based on the desired type...

Dim value1 As Object = Convert.ChangeType(TESTENUM.AA,
GetType(String))
Dim value2 As Object = Convert.ChangeType(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**************@tk2msftngp13.phx.gbl...
| This is because of an implicit cast ?
|
| --
| Best Regards
|
| The Inimitable Mr Newbie º¿º
|
|
|
| "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
| news:uy**************@TK2MSFTNGP12.phx.gbl...
| > Hi,
| >
| > Yes it is. The enums tostring will return the name of the enum.
| >
| >
| > Ken
| > ----------------
| > "Barry" <bc******@castcom.com> wrote in message
| > news:2m********************************@4ax.com...
| >> 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.DataRow
| >> Dim cc As System.Data.DataColumn
| >> Dim tt As System.Data.DataTable
| >>
| >> tt = New System.Data.DataTable
| >>
| >> cc = New System.Data.DataColumn
| >> With cc
| >> .AllowDBNull = False
| >> .DataType = System.Type.GetType("System.String")
| >> .ColumnName = "string"
| >> End With
| >> tt.Columns.Add(cc)
| >>
| >> cc = New System.Data.DataColumn
| >> With cc
| >> .AllowDBNull = False
| >> .DataType = System.Type.GetType("System.Int32")
| >> .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.TESTENUM.AA
| >>
| >> System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("string").ToString)
| >> .Rows(0).Item("integer") = testclass2.TESTENUM.AA
| >>
| >> System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("integer").ToString)
| >> End With
| >>
| >> End Sub
| >>
| >>
| >>
| >> bceggersATcomcastDOTnet
| >
| >
|
|
Dec 24 '05 #4
Barry,
In addition to the other comments.

DataRow will convert the input value into the respective
DataColumn.DataType. As I told Mr Newbiew, I strongly suspect that DataRow
is using Convert.ChangeType 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.ChangeType(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.GetType("System.String")
.DataType = GetType(String)
| .DataType = System.Type.GetType("System.Int32")
.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******@castcom.com> wrote in message
news:2m********************************@4ax.com...
| 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.DataRow
| Dim cc As System.Data.DataColumn
| Dim tt As System.Data.DataTable
|
| tt = New System.Data.DataTable
|
| cc = New System.Data.DataColumn
| With cc
| .AllowDBNull = False
| .DataType = System.Type.GetType("System.String")
| .ColumnName = "string"
| End With
| tt.Columns.Add(cc)
|
| cc = New System.Data.DataColumn
| With cc
| .AllowDBNull = False
| .DataType = System.Type.GetType("System.Int32")
| .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.TESTENUM.AA
|
| System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("string").ToString)
| .Rows(0).Item("integer") = testclass2.TESTENUM.AA
|
| System.Windows.Forms.MessageBox.Show(.Rows(0).Item ("integer").ToString)
| End With
|
| End Sub
|
|
|
| bceggersATcomcastDOTnet
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.DataType. As I told Mr Newbiew, I strongly suspect that DataRow
is using Convert.ChangeType 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.ChangeType(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.GetType("System.String")
.DataType = GetType(String)
| .DataType = System.Type.GetType("System.Int32")
.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...


bceggersATcomcastDOTnet
Dec 24 '05 #6

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

Similar topics

2
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...
3
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...
1
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...
2
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...
6
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...
13
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
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...
3
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
11
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.