Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on 

Typcasting an enum to an int

I have an enum type that starts at 0 and goes up, andit matches integer values in a table.  So I need to convert the enum to an int.

What's a safe way to to this?

Is it as easy as:

public enum MyEnumValues
{
    myFirstVal=0,
    mySecondsVal,
}

int mappedValue = (int)MyEnumValues.mySecondVal;

I want mappedValue  to be 1. Will this be the case as coded?  How else can I code this?

thanks,
newbieweb
.NET Programming

Avatar of undefined
Last Comment
ororiole
ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ororiole
ororiole
Flag of United States of America image

Some more info you may find usefule. If you dont know the underlying type of the enum you can get it with:
Type t = Enum.GetUnderlyingType(typeof(MyEnumValues));

And I got carried away, disregard what I said about float. Enums cannot be based on float. They can only be based on integral types: int, short, etc. They may be based on any integral type except char.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo