Re: Casting of Enum values

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jason Larion (jason.larion_at_gmail.com)
Date: 01/30/05


Date: 30 Jan 2005 13:14:42 -0800

Chris (et al),

That's exactly the surprising behaviour... I would think that anything
that calls ToString on it would return a string-formatted object of the
_value_, not the attribute.

I.e., in the example. "1" as String. Why do I get the attribute as
string? That's the part that I'm struggling with.

Thanks,
--Jason

Chris, Master of All Things Insignificant wrote:
> AFAIK, The debug.writeline will make a call to the ToString() method
of the
> object passed in. If you want to see the number try this.
>
> Debug.WriteLine(Cint(Cheese.Cheddar))
>
> Didn't test it but it should write out the integer value of the enum.
>
> Chris
>
>
> "Jason Larion" <jason.larion@gmail.com> wrote in message
> news:1106942373.695332.139900@z14g2000cwz.googlegroups.com...
> > 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
> >



Relevant Pages

  • Re: Function to return more than one value
    ... Chris, ... But is it true that Enum cannot contain string ... > dim Height as Integer ...
    (microsoft.public.access.modulesdaovba)
  • Re: Macro to stringify an enum
    ... Chris wrote: ... > string using the number I've assigned to and concatenting a string to ... > typedef enum ... > int main ...
    (comp.lang.c)
  • Re: enums: What the H? again
    ... > find no sign of it in the Enum class until I did a decompile and saw ... > private String shortName; ... > private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • Re: enums: What the H? again
    ... Everything comes clear when you decompile an enum, ... private String shortName; ... private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • A Class to get the name of Enum constant at runtime
    ... Dim rs As ADODB.Recordset ... You can get the description of an Enum form it's value. ... EnumName As String, ... Dim Idx As Long ...
    (microsoft.public.vb.general.discussion)