469,588 Members | 2,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

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 2685
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Voronkov Konstantin | last post: by
3 posts views Thread by Richard | last post: by
2 posts views Thread by Dennis | last post: by
6 posts views Thread by Jason Larion | last post: by
3 posts views Thread by shapper | last post: by
11 posts views Thread by =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.