ENUM is string or integer ??

B

Barry

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
 
M

Mr Newbie

This is because of an implicit cast ?

--
Best Regards

The Inimitable Mr Newbie º¿º
 
J

Jay B. Harlow [MVP - Outlook]

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


| This is because of an implicit cast ?
|
| --
| Best Regards
|
| The Inimitable Mr Newbie º¿º
|
|
|
| | > Hi,
| >
| > Yes it is. The enums tostring will return the name of the enum.
| >
| >
| > Ken
| > ----------------
| > | >> 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
| >
| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

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


| 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
 
B

Barry

Thanks for the tip....

And thanks to all for the answers !!!


Merry Christmas !!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top