473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Should I use an Enum or a List?

HaLo2FrEeEk
404 256MB
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.
Jun 5 '10 #1
4 2643
GaryTexmo
1,501 Expert 1GB
You could use a combination...

Expand|Select|Wrap|Line Numbers
  1. public enum ErrorType
  2. {
  3.   Error0,
  4.   Error1
  5.   ...
  6. }
  7.  
  8. string[] errors = new string[] { "this is error 0", "this is error 1", ... }
  9.  
  10. string errText = errors[ErrorType.Error0];
  11.  
Would that work?
Jun 5 '10 #2
To me it sounds like a dictionary is what you want.
Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, string> data = new Dictionary<int, string>();
  2. data.Add(30, "The string for 30");
  3. string value = data[30];   //gets the string associated with the key 30. in this case "The string for 30"
  4.  
Jun 5 '10 #3
HaLo2FrEeEk
404 256MB
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?
Jun 5 '10 #4
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.
Expand|Select|Wrap|Line Numbers
  1. string mapFromId(int id)
  2. {
  3. switch(id)
  4. {
  5. case 30:
  6. return "Last Resort";
  7. default:
  8. return "Invalid Map";
  9. }
  10. }
  11.  
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.
Jun 7 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
2
by: mrhicks | last post by:
Hello all, I have a question about enumerations. Within some requirements data passed back a certain bit field is defined by three bits then in another section the bit field is defined as 4...
2
by: David | last post by:
Hi, namespace EnumTest { class Class1 { public enum num { one = 1, uno = 1,
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
5
by: genc_ ymeri at hotmail dot com | last post by:
well, I making up a "scenario" which may not be the best but I can make my point at least techincally..,. I have this enum : public enum JustTest { Volvo = 0x32324234, Acura = 0x32423443,...
13
by: Jorn Ronnow | last post by:
I'm a bit confused here, since the help in M$ Visual Studio 2005 states the syntax for enum as: enum {enum-list} ; So, it would be possible to write: enum Protocol_t : unsigned char {...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm trying to say something like the following but tripping up on syntax. Basically, i'm trying to expose an enum that's inside my webusercontrol to my client which is my .aspx page ...
5
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
he yall, does anyone know the best way to populate a dropdownlist from an enum list? thanks, rodchar
16
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
13
by: Rohit | last post by:
Is there any way by which I can find number of elements in an enum list? Generally I use one last element at the end to find out total number of elements. e.g. typedef enum{ SUN, MON, TUE,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.