Reputation: 9247
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
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
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
Reputation: 17868
Have you tried something like
Game item = Game.LiveBetting;
String itemselected= Enum.GetName(typeof(Game), item);
Upvotes: 0