Re: Casting of Enum values
From: Jason Larion (jason.larion_at_gmail.com)
Date: 01/30/05
- Next message: Bob: "Re: Could not copy temporary files to the output directory"
- Previous message: james: "Re: Date from Number of Days"
- In reply to: Chris, Master of All Things Insignificant: "Re: Casting of Enum values"
- Next in thread: Phill. W: "Re: Casting of Enum values"
- Reply: Phill. W: "Re: Casting of Enum values"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: Casting of Enum values"
- Messages sorted by: [ date ] [ thread ]
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
> >
- Next message: Bob: "Re: Could not copy temporary files to the output directory"
- Previous message: james: "Re: Date from Number of Days"
- In reply to: Chris, Master of All Things Insignificant: "Re: Casting of Enum values"
- Next in thread: Phill. W: "Re: Casting of Enum values"
- Reply: Phill. W: "Re: Casting of Enum values"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: Casting of Enum values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|