Basic enum question. 
Author Message
 Basic enum question.

Hi,

In this simple example the enum returns the constant and not the integer
value I assigned it? Isn't it supposed to give me the value?

// created on 11/12/2001 at 7:05 PM
using System;

class HelloWorld2
{

enum temper : uint
{
 cold = 0,
 freezing = 32,
 swimtemp = 80,

Quote:
}

public static void Main()
 {

 Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives me
the back 'freezing' and not 32?

 }

Quote:
}



Sat, 01 May 2004 11:01:49 GMT  
 Basic enum question.
don't be confused with the "value" of an object and it ToString() method.

Quote:
>  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives me
> the back 'freezing' and not 32?

enum have a nice ToString() default method which return the string literal.
anyway freezing is 32 !


Sat, 01 May 2004 10:48:21 GMT  
 Basic enum question.
Ok,

So how do I get it to spit out 32?

Thanks,

Stefan


Quote:
> don't be confused with the "value" of an object and it ToString() method.

> >  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives
me
> > the back 'freezing' and not 32?

> enum have a nice ToString() default method which return the string
literal.
> anyway freezing is 32 !



Sat, 01 May 2004 12:01:36 GMT  
 Basic enum question.
e.g.
Console.WriteLine("How cold is cold: {0}", (int) temper.freezing );
Quote:

> So how do I get it to spit out 32?



Sat, 01 May 2004 16:02:30 GMT  
 Basic enum question.
A couple notes on that...

    1. That shouldn't be a uint.
        A. It is not CLS compliant.
        B. You cannot later add in lower values such as absolute 0.
(about -4,000 if I remember correctly)
        C. It does not support fractions of a degree.
    2. That should be a struct with constants, not be a enum.
        A. It represents a continuous range, not just those three named
values.
        B. It does not allow for conversions between scales.

I would suggest creating a struct such as...

Structure Temperature
    Enum Scale
        Kelvin
        Celsius
        Fahrenheit
    End Enum

    Shared Public ReadOnly Property Get Freezing As Temperature

    Private m_nValue as Decimal //Always in Kalvin

    Public Sub New (value as Decimal, scale as TemperatureScale)

    Public Function GetValue(scale as TemperatureScale) As Decimal

    Public ReadOnly Property Kelvin As Decimal
    Public ReadOnly Property Celsius As Decimal
    Public ReadOnly Property Fahrenheit As Decimal

    Public Function Add(value as Decimal, scale as TemperatureScale) as
Temperature
    Public Function Subtract(value as Decimal, scale as TemperatureScale) as
Temperature

End Structure
--
Jonathan Allen


Quote:
> Hi,

> In this simple example the enum returns the constant and not the integer
> value I assigned it? Isn't it supposed to give me the value?

> // created on 11/12/2001 at 7:05 PM
> using System;

> class HelloWorld2
> {

> enum temper : uint
> {
>  cold = 0,
>  freezing = 32,
>  swimtemp = 80,
> }

> public static void Main()
>  {

>  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives me
> the back 'freezing' and not 32?

>  }
> }



Sat, 01 May 2004 18:05:25 GMT  
 Basic enum question.

Quote:
> don't be confused with the "value" of an object and it ToString() method.

But the value is 'freezing'. It only truly becomes 32 when it is cast into
an integer.

--
Jonathan Allen


Quote:
> don't be confused with the "value" of an object and it ToString() method.

> >  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives
me
> > the back 'freezing' and not 32?

> enum have a nice ToString() default method which return the string
literal.
> anyway freezing is 32 !



Sat, 01 May 2004 18:25:01 GMT  
 Basic enum question.
That code looks like vb.net to me?

Stefan


Quote:
> A couple notes on that...

>     1. That shouldn't be a uint.
>         A. It is not CLS compliant.
>         B. You cannot later add in lower values such as absolute 0.
> (about -4,000 if I remember correctly)
>         C. It does not support fractions of a degree.
>     2. That should be a struct with constants, not be a enum.
>         A. It represents a continuous range, not just those three named
> values.
>         B. It does not allow for conversions between scales.

> I would suggest creating a struct such as...

> Structure Temperature
>     Enum Scale
>         Kelvin
>         Celsius
>         Fahrenheit
>     End Enum

>     Shared Public ReadOnly Property Get Freezing As Temperature

>     Private m_nValue as Decimal //Always in Kalvin

>     Public Sub New (value as Decimal, scale as TemperatureScale)

>     Public Function GetValue(scale as TemperatureScale) As Decimal

>     Public ReadOnly Property Kelvin As Decimal
>     Public ReadOnly Property Celsius As Decimal
>     Public ReadOnly Property Fahrenheit As Decimal

>     Public Function Add(value as Decimal, scale as TemperatureScale) as
> Temperature
>     Public Function Subtract(value as Decimal, scale as TemperatureScale)
as
> Temperature

> End Structure
> --
> Jonathan Allen



> > Hi,

> > In this simple example the enum returns the constant and not the integer
> > value I assigned it? Isn't it supposed to give me the value?

> > // created on 11/12/2001 at 7:05 PM
> > using System;

> > class HelloWorld2
> > {

> > enum temper : uint
> > {
> >  cold = 0,
> >  freezing = 32,
> >  swimtemp = 80,
> > }

> > public static void Main()
> >  {

> >  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives
me
> > the back 'freezing' and not 32?

> >  }
> > }



Sun, 02 May 2004 04:37:07 GMT  
 Basic enum question.
It is my pseudo-code, which has been heavily influenced by VB/VB.Net. It
should be easy for you to translate into C# code.

One note: If you overload + and -, make it take a Temperature and a Decimal,
not two temperatures. (I do not know if this is possible in C#, but I think
it is.)

--
Jonathan Allen


Quote:
> That code looks like vb.net to me?

> Stefan



> > A couple notes on that...

> >     1. That shouldn't be a uint.
> >         A. It is not CLS compliant.
> >         B. You cannot later add in lower values such as absolute 0.
> > (about -4,000 if I remember correctly)
> >         C. It does not support fractions of a degree.
> >     2. That should be a struct with constants, not be a enum.
> >         A. It represents a continuous range, not just those three named
> > values.
> >         B. It does not allow for conversions between scales.

> > I would suggest creating a struct such as...

> > Structure Temperature
> >     Enum Scale
> >         Kelvin
> >         Celsius
> >         Fahrenheit
> >     End Enum

> >     Shared Public ReadOnly Property Get Freezing As Temperature

> >     Private m_nValue as Decimal //Always in Kalvin

> >     Public Sub New (value as Decimal, scale as TemperatureScale)

> >     Public Function GetValue(scale as TemperatureScale) As Decimal

> >     Public ReadOnly Property Kelvin As Decimal
> >     Public ReadOnly Property Celsius As Decimal
> >     Public ReadOnly Property Fahrenheit As Decimal

> >     Public Function Add(value as Decimal, scale as TemperatureScale) as
> > Temperature
> >     Public Function Subtract(value as Decimal, scale as
TemperatureScale)
> as
> > Temperature

> > End Structure
> > --
> > Jonathan Allen



> > > Hi,

> > > In this simple example the enum returns the constant and not the
integer
> > > value I assigned it? Isn't it supposed to give me the value?

> > > // created on 11/12/2001 at 7:05 PM
> > > using System;

> > > class HelloWorld2
> > > {

> > > enum temper : uint
> > > {
> > >  cold = 0,
> > >  freezing = 32,
> > >  swimtemp = 80,
> > > }

> > > public static void Main()
> > >  {

> > >  Console.WriteLine("How cold is cold: {0}", temper.freezing); // gives
> me
> > > the back 'freezing' and not 32?

> > >  }
> > > }



Sun, 02 May 2004 05:19:27 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Basic questions about CLSCTX enum, inproc and out-of-proc servers

2. typedef enum as subset of another enum

3. enum - enum ?

4. enum - enum ?

5. newbie enum questions

6. enum: syntax question

7. enum questions

8. Newbie question: Enum for States

9. Quick Enum Question

10. Question of enum

11. enum & binary question

12. Question on enum

 

 
Powered by phpBB® Forum Software