Enum question

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hi,

stupid question: Why can´t i do this:

public enum eMy: byte {
Item1 = 1,
Item2 = 127,
Item3 = 128,
Item4 = 255,
}

void MyFunc() {
eMy b = eMy.Item3;
Foo( b );
}

void Foo( object o ) {
int i = ( int )o;
}

I get the "Specified cast is not valid."-exception. I must do double cast:

int i = ( int )( eMy )o;

And another thing: if i look the variable b in Debugger - it shows that the
value is "-128" not 128? Its a little confusing - when i code something like
this...

static void Foo( object o ) {
int i = (int)(eMy)o;

eMy d = eMy.Item3;
if( i == ( int )d ) {
// we go here
}
}

....and debug it: it clearly states that "c" is "-128" and "i" is "128" - i
== d -> 128 == -128!


Kimmo Laine
 
Kimmo Laine said:
stupid question: Why can´t i do this:

public enum eMy: byte {
Item1 = 1,
Item2 = 127,
Item3 = 128,
Item4 = 255,
}

void MyFunc() {
eMy b = eMy.Item3;
Foo( b );
}

void Foo( object o ) {
int i = ( int )o;
}

I get the "Specified cast is not valid."-exception. I must do double cast:

int i = ( int )( eMy )o;

Yup - because when you box a value, its exact type is boxed with it. In
this case, the boxed type is eMy. When you unbox, you have to unbox to
the same type, and *then* you can do conversions.
And another thing: if i look the variable b in Debugger - it shows that the
value is "-128" not 128? Its a little confusing - when i code something like
this...

static void Foo( object o ) {
int i = (int)(eMy)o;

eMy d = eMy.Item3;
if( i == ( int )d ) {
// we go here
}
}

...and debug it: it clearly states that "c" is "-128" and "i" is "128" - i
== d -> 128 == -128!

b should certainly not be shown as -128 - that's not a valid value for
a byte in the first place. Not sure what you mean about "c" in your
description though...
 
My bad! "c" = "d"!

- Kimmo



Kimmo Laine said:
stupid question: Why can´t i do this:

public enum eMy: byte {
Item1 = 1,
Item2 = 127,
Item3 = 128,
Item4 = 255,
}

void MyFunc() {
eMy b = eMy.Item3;
Foo( b );
}

void Foo( object o ) {
int i = ( int )o;
}

I get the "Specified cast is not valid."-exception. I must do double cast:

int i = ( int )( eMy )o;

Yup - because when you box a value, its exact type is boxed with it. In
this case, the boxed type is eMy. When you unbox, you have to unbox to
the same type, and *then* you can do conversions.
And another thing: if i look the variable b in Debugger - it shows that
the
value is "-128" not 128? Its a little confusing - when i code something
like
this...

static void Foo( object o ) {
int i = (int)(eMy)o;

eMy d = eMy.Item3;
if( i == ( int )d ) {
// we go here
}
}

...and debug it: it clearly states that "c" is "-128" and "i" is "128" - i
== d -> 128 == -128!

b should certainly not be shown as -128 - that's not a valid value for
a byte in the first place. Not sure what you mean about "c" in your
description though...
 
that may just be something the debugger is doing, and may not be reflective
of the actual logic.

--- Nick

Kimmo Laine said:
stupid question: Why can´t i do this:

public enum eMy: byte {
Item1 = 1,
Item2 = 127,
Item3 = 128,
Item4 = 255,
}

void MyFunc() {
eMy b = eMy.Item3;
Foo( b );
}

void Foo( object o ) {
int i = ( int )o;
}

I get the "Specified cast is not valid."-exception. I must do double cast:

int i = ( int )( eMy )o;

Yup - because when you box a value, its exact type is boxed with it. In
this case, the boxed type is eMy. When you unbox, you have to unbox to
the same type, and *then* you can do conversions.
And another thing: if i look the variable b in Debugger - it shows that the
value is "-128" not 128? Its a little confusing - when i code something like
this...

static void Foo( object o ) {
int i = (int)(eMy)o;

eMy d = eMy.Item3;
if( i == ( int )d ) {
// we go here
}
}

...and debug it: it clearly states that "c" is "-128" and "i" is "128" - i
== d -> 128 == -128!

b should certainly not be shown as -128 - that's not a valid value for
a byte in the first place. Not sure what you mean about "c" in your
description though...
 

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

Similar Threads


Back
Top