0

First, i'm a beginning in c#. I'm try to code some game. I don't know how to return enum value as string.

Here my code.

public class CARDS {

    public CARDS(int id, int atk, ClassType ctype, string name) {
        this.CARD_ID = id;
        this.C_TYPE = ctype;
        this.ATK = atk;
        this.NAME_EN = name;
    }
     public CARDS() {
          this.CARD_ID = -1;
     }

     public int CARD_ID { get; set; }   
     public ClassType C_TYPE { get; set; }
     public int ATK { get; set; }
     public string NAME_EN { get; set; }

     public enum ClassType {
          Warrior,
          Mage,
          Archer,
          Thief,
          Bishop,
          Monk,
          Guardian,
          Destroyer,
          Chaser,
          Hermit,
          Alchemy   
     }
}

....... Here I try to do.

public class CardCollection : MonoBehaviour {
     private List<CARDS> dbase = new List<CARDS>();
     private JsonData cardsdata;
     private JsonData card;

     void Start() {
          cardsdata = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Json/card.json"));
          ConstructCardData();
          Debug.Log(dbase[1].NAME_EN + " " + dbase[23].NAME_EN);
     }
     void ConstructCardData() {
          card = cardsdata["CARDS"];
          for (int i = 0; i < card.Count; i++) {
               dbase.Add(new CARDS((int)card[i]["CARD_ID"], (int)card[i]["ATK"], card[i]["C_TYPE"].ToString(), card[i]["NAME_EN"].ToString()));
          }
     }
}

// card[i]["C_TYPE"].ToString() It say can't convert from string to CARDS.ClassType

  • i see 2 constructors, some properties, and an enum. I don't see you trying to `return enum value as string`. Can you show what you are trying to do? – Jonesopolis Aug 05 '16 at 05:34

2 Answers2

0

What about :

public class CARDS
{
    public CARDS(int id, int atk, ClassType ctype, string name)
    {
        this.CARD_ID = id;
        this.C_TYPE = Enum.GetName(ctype.GetType(), ctype); //Use Enum.GetName to get string
        this.ATK = atk;
        this.NAME_EN = name;
    }
    public CARDS()
    {
        this.CARD_ID = -1;
    }

    public int CARD_ID { get; set; }
    public string C_TYPE { get; set; } //change type to string
    public int ATK { get; set; }
    public string NAME_EN { get; set; }

    public enum ClassType
    {
        Warrior,
        Mage,
        Archer,
        Thief,
        Bishop,
        Monk,
        Guardian,
        Destroyer,
        Chaser,
        Hermit,
        Alchemy
    }
}
Harry Birimirski
  • 858
  • 1
  • 8
  • 21
0

ToString() on the enum values return the string value of the enum. Custom string values can also be returned for the enum values, check these links, link1 , link2

Examples:

ClassType.Warrior.ToString();

ctype.ToString();
Community
  • 1
  • 1
KDR
  • 478
  • 6
  • 19