enums ... what am I doing wrong?

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

Newbie...

I'm casting enums back and forth to ints so to use them as an index into a
complex two dimensional array. I thought it would help readability.
Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!
 
Why are you casting them in the first place? :)

public enum ConnectionStatus : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting, Disconnected
}
ShaneB
 
ShaneB,

There is no implicit cast for enums to their underlaying type.
public enum ConnectionStatus : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting, Disconnected
}

In fact, despite of the that syntax c# uses, ConnectionStatus doesn't
inherit from *int*

Thus, in the case of using enum values for indexing arrays casting is
necessary.
 
Actually, that post should have been canceled. Two seconds after I hit
send, I realized my mistake :)

ShaneB
 
Craig said:
Newbie...

I'm casting enums back and forth to ints so to use them as an index
into a complex two dimensional array. I thought it would help
readability. Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!

How about using constants instead of enum values?
* same readability of enum (words instead of "magic numbers")
* they really are int's

public const int WHATEVER = 3;
myval = store[WHATEVER];

Hans Kesting
 
Back
Top