Should I use an Enum or a List?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Should I use an Enum or a List?

    I have a set of data that consists of a a list of unique numeric identifiers corresponding to a string title. I will be reading a list of files and in each file is one of the numeric identifiers at a specific offset. I want to read that information and use the numeric ID to provide the user with the corresponding string title.

    I thought about using an enum, but my tests with enum didn't go so well, and I don't really understand how they work. Also, you can't use strings in enum's, right? On the flip side, there are 57 individual items, and they're categorized further in 3 categories (according to another piece of data I'm reading from the files.)

    My question is, what will be the best way to store this data in my program so that I can read, for example, the value 30 from the file and have the program return the string corresponding to the identifier 30. List, array, enum? Some other method I haven't thought of? I'm totally open to suggestions, especially if it involves learning something new, so please, help me out.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You could use a combination...

    Code:
    public enum ErrorType
    {
      Error0,
      Error1
      ...
    }
    
    string[] errors = new string[] { "this is error 0", "this is error 1", ... }
    
    string errText = errors[ErrorType.Error0];
    Would that work?

    Comment

    • dschu012
      New Member
      • Jul 2008
      • 39

      #3
      To me it sounds like a dictionary is what you want.
      Code:
      Dictionary<int, string> data = new Dictionary<int, string>();
      data.Add(30, "The string for 30");
      string value = data[30];   //gets the string associated with the key 30. in this case "The string for 30"

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Basically I'm working on a program which deals with files from a video game. These files have the details of which map in the game they are from (the files are user-generated.) My program opens these files and gets the information out of them, and I want to be able to read the id number that represents the map the file was created on and report that back in my program, so in my case the id 30 would represent the map name "Last Resort" in Halo 3.

        Would a dictionary be best for this? Since there are 57 individual items is there a smaller way of achieving this, without requiring that the user download any secondary files that they might delete or modify. Basically I don't want to add 57 more lines of code to my program for each map name / id number combo. Could I do it in a seperate class file?

        Comment

        • dschu012
          New Member
          • Jul 2008
          • 39

          #5
          I can't really say if something is the "best" way. I would use an xml file to store the data but since you don't want to have extra files, why not just do something like this.
          Code:
          string mapFromId(int id)
          {
          switch(id)
          {
          case 30:
          return "Last Resort";
          default:
          return "Invalid Map";
          }
          }
          If you want to do it in another class sure go ahead, but I don't see any reason why to not just add the lines of code. You can always hide the function with the + and - signs on the left of the code view.

          Comment

          Working...