Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Casting enumerations

Just curious really. If I declare an enumeration of type int, why do I have to cast it to an int when I want to use it as an int?

public enum ResultType : int

string value1 = reader.GetString(Col1); // fails
string vales2 = reader.GetString((int)Col2); // works fine

Why does the first GetString fail? Isn't Col1 an int?

Mark B
Friday, April 15, 2005

no, Col1 is of type ResultType.

Matt B
Friday, April 15, 2005

The enumeration syntax doesn't imply that you are deriving from int, since that's impossible. It's merely specifying the underlying storage mechanism for the enumeration.

The fact that it looks like derivation is unfortunate but understandable.

Brad Wilson [MSFT]
Saturday, April 16, 2005

You should use ResultType.Col1 then you don't need to cast.

Johnny Moondog
Wednesday, April 20, 2005

*  Recent Topics

*  Fog Creek Home