J
Jason Larion
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
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