None
None

Reputation: 9247

How to get integer value and convert to string from enumeration?

I have this class:

public class GameTypeModel
{
    public string Value { get; set; }
    public string TransKey { get; set; }             
}

I have this foreach:

GameType.Add(new GameTypeModel() { Value = "", TransKey = "ALL" });
foreach (Game val in Enum.GetValues(typeof(Game)))
{      
     GameType.Add(new GameTypeModel() { Value = value, TransKey = val.ToString() });
}

I have this enumeration:

public enum Game 
{
    [EnumMember]
    Terminal = 0, 
    [EnumMember]
    SportsBetting = 1,
    [EnumMember]
    LiveBetting = 2,
}

How can i use this numbers and pass it to Value like a string?

Upvotes: 0

Views: 69

Answers (4)

Dennis
Dennis

Reputation: 37780

If you want to store "0", "1", "2" in GameTypeModel.Value, then just cast enum value to its underlying type (it is int by default):

new GameTypeModel { Value = ((int)val).ToString(), TransKey = val.ToString() }

Upvotes: 1

Jan Unld
Jan Unld

Reputation: 370

Easy thing mate. You'll have to cast the values to int before you convert them into string. Try this..

public enum Game : int {
    Terminal = 0,
    // ...
}

// add the explicit cast to int here
GameType.Add(
    new GameTypeModel() { 
        Value = value, 
        TransKey = ((int)val).ToString() 
    });

Upvotes: 0

Mick3y
Mick3y

Reputation: 46

Have you tried this: .ToString()to get the value ?

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

Have you tried something like

    Game item = Game.LiveBetting;
    String itemselected= Enum.GetName(typeof(Game), item);

Upvotes: 0

Related Questions