I want to basically do a look up table in code, whether its array of strings , enumeration, or dictionary, im trying to find the best way.
I want to do lookup base off of the code, so when I extact data in and I get a string that is "000", I want to say "000" is cat. which is the best way to do this
item code
cat , "000"
dog , "001"
mice, "01A"
Hi you guys what do you think is the best way to do this, right now my lookup table is in the database and im pulling from the database which is cool, but im trying to limit the database from doing all the work.
Loading
David SmithPosted Oct 17, 2010, 8:46 PM
Thanks , instead I just kept it simple, i was trying to broaden my c sharp vocabulary i did something like this instead
This works perfect I cut down a minute in a half when processing files, Im trying to stay from opening the database as much as possible
public class commonAnimals
{
static private Dictionary<string, string> ValidAnimalsNames= new Dictionary<string,string>();
static public void initDictionary()
{
ValidAnimalsNames.Add("0000", "CAT");
ValidAnimalsNames.Add("00AA", "DOG");
ValidAnimalsNames.Add("0002", "BEAR");
}
static public string GetValidAnimalsName(string key)
{
string value = string.Empty;
if (ValidAnimalsNames.ContainsKey(key))
{
value = ValidAnimalsNames[key];
}
return value;
}
}
Sam HobbsPosted Oct 15, 2010, 6:01 PM
David SmithPosted Oct 15, 2010, 5:10 PM
Sam HobbsPosted Oct 15, 2010, 4:59 PM
David SmithPosted Oct 15, 2010, 4:24 PM
Sam HobbsPosted Oct 15, 2010, 4:03 PM
If the identifying code is a sequential number such as shown in your latest sample code, then you could create an array of strings (Descriptions) and then use the subscript to get a specific Description. If the code is not sequential but it is (or could be) an integer, then you could use a Dictionary or a collection such as that. You could use a Dictionary or a collection such as that even if the key is a string.
Felipe RamosPosted Oct 15, 2010, 11:27 AM
David SmithPosted Oct 15, 2010, 11:02 AM
public enum ValidAnimals
{
[Description("BEAR")] A = 0x0000, //is there a way to get the description base off of the 0x0000
[Description("DOG ")] B = 0x0001,
[Description("CAT ")] C = 0x0002,
}
public string GetName(string Value)
{Value ="0000";
FieldInfo fi = stringValue.GetType().GetField(Value);
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : stringValue;
}
Felipe RamosPosted Oct 15, 2010, 9:41 AM