Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enums and list indexes

Discussion in 'Scripting' started by Ghidera, Apr 5, 2014.

  1. Ghidera

    Ghidera

    Joined:
    Mar 6, 2013
    Posts:
    53
    I can't seem to figure out how to use an enum as an index value in c# for a list, I get an error like can't convert enum to int.
    Code (csharp):
    1.  
    2. public Enum MsgType {
    3. Normal,
    4. Warn,
    5. Panic
    6. }
    List<string> myMessages = new List<string>;

    ... later on

    Code (csharp):
    1.  
    2. void LogMessage(MsgType mType) {
    3.  
    4. myMessages.Add("Everything is great!");
    5. myMessages.Add("Warning: Running low on Mountain Dew.");
    6. myMessages.Add("PANIC! We're out of Mountain Dew.");
    7.  
    8. Debug.Log(myMessages[mType]);
    9.  
    10. }
    11.  

    Of course myStrings[] wants an integer for the index and it's getting an enum which is apparently not an integer. I see no #CONST or anything. Is there a way to use enums for indexes or am I stuck making int variables? I really don't want my code to say things like

    LogMessage(3);

    Mostly because there's no way I'll remember what number to use for all the different elements.


    Thanks!

    Edit: Didn't see code tags in the toolbar, now I see "go advanced" has them
     
    Last edited: Apr 5, 2014
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    please use code tags!

    myMessages[(int)mType];
    there is no implicit conversion from enum to int so do it explicitely. note: when you assign manual values (ie Normal = 5) you leave out some indices in the list so avoid that.
     
  3. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    925
    As an alternative you could use a dictionary and index the values with your enum.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    but you should also provide your own equalitycomparer when doing so as the default comparer is really slow (approx factor 10) because it boxes the value type.
     
  5. Ghidera

    Ghidera

    Joined:
    Mar 6, 2013
    Posts:
    53
    I have used C type languages before but I've not run into the need for casting enums. I've just gotten into C# for unity after using VB.net for a couple of years so it's taking a while to get back into the groove. I'd totally forgotten about casting in that manner.

    Of course if I plan on using the enum value in multiple places I'll cast the value into an int variable first and use the int everywhere.

    Thanks for the reminder.
     
  6. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    That would certainly be less leaky and a much more correct way of doing something like this.

    Yup. The fact that enums don't implement IEquatable<> should really be considered a serious bug or flaw in the .Net spec. And in Unity, just calling the getHashCode() method of an enum value causes allocations (!).