-1

I want to be able to associate everything in my program with the original integer that it is found as, but in the end, I want to be able to output a specific string based on that integer number. Right now all of the integers that are found by my program are added to a list, so I was wondering if there was a way that I could use a foreach() loop for each item T in that list and then add a corresponding string to another list.

Is there a way I can do this with Enums or any other build in functions in c#? Ideally, I would want something that looks like this:

public enum Tools
{
    tool1 = 1001,
    tool2,
    tool3,
    tool4
}

foreach (int T in ToolList)
{
    //I want to get the enum value based on which int value is passed.
    strToolList.add((int).Tools); 
}

I think I would then be able to later convert the tool names to strings for output with a separate function, but if anyone knows how to do that while accomplishing this as well, that would be useful.

Thanks in advance!

Jacob Varner
  • 340
  • 1
  • 4
  • 16
  • To convert enum to int, do `(Tools)T` – Nolonar Jun 19 '13 at 14:47
  • Did you try googling this? There are already hundreds of questions on SO about enums. – Brad M Jun 19 '13 at 14:48
  • possible duplicate of [C# String enums](http://stackoverflow.com/questions/424366/c-sharp-string-enums) – Brad M Jun 19 '13 at 14:48
  • @BradM I have googled it and looked all over this site, but I have not been able to put together a method that actually does what I want. I want to add a corresponding enum (or ideally string value) to another list based on which int value is found. I know that the enums are associated with a string, but I didn't find a way to essentially "call" them based on their int value. – Jacob Varner Jun 19 '13 at 14:49

1 Answers1

0
foreach (int T in ToolList)
{
   strToolList.add(Enum.GetName(typeof(Tools), (Tools)T)); 
}
Chris
  • 1,610
  • 3
  • 18
  • 37