Enumerations are extremely helpful when it comes to keep data in order, but there’s no set way to figure out the index of something that’s in an enum. So, let’s say for example your loading in a group of 1,000 enemies from a text file and each one has a type that you use to grab their textures (or models) and whatever. Well, we can put their numeric type right in the file, but then if we add a new enemy it would HAVE to go at the end of the list. Part of the beauty of using enum’s is that you can add something anywhere and the numbers will adjust. So, here is a little function I wrote to search through an enum and return the index of whatever you were searching for.
First off, let’s look at the function. I made mine static so that it could be called from anywhere, but the important part is the parameters.
public static int getIndexOf(string name, bool ignoreCase, Type enumeration)
“name” – This is what you are searching for. Maybe better named “searchTerm” or something along those lines.
“ignoreCase” – If what you are searching for should be case-sensitive or not. Obviously this could be left out if you name all your enums lowercase or whatever, but I think it’s a nice touch and makes the function more versatile.
“enumeration” – This is the enum you want to search through, however, there is a little trick here. You can’t pass a straight enum, you have to pass the “typeof” the enum.
Example Enum:
enum GreasyFoods{
Pizza,
FrenchFries,
Hamburger,
CornDog
}
If that’s our enum, then we’d call the function like so:
getIndexOf("hamburger", true, typeof(GreasyFoods));
And then that would return to us ‘2’ because Pizza = 0, FrenchFries = 1, and Hamburger = 2. (note that to call a static function you have to call it via the name of the class it’s part of.)
Let’s have a look inside the function now, piece by piece. First step is to get the names of everything in the enum so that we can loop through it and look for the part we are searching for. C# makes this nice and easy on us.
string[] names = Enum.GetNames(enumeration);
This uses a built in function that’s part of the Enum class to return to us a string array containing all the names in the enum. We now have them stored in our own string array created called ‘names’. Next, we’ll loop through it searching for the name passed into the function.
for (int i = 0; i < names.Length; i++)
{
if (names[i] == name)
{
return i;
}
}
Just like that we’ll get back the index of the name we searched for. Now, in the final version of my function below I added in the check for ‘ignoreCase’, it’s pretty simple though and should be easy to understand. Also, at the end of the function I added in a ‘return -1’ incase the index is not found then -1 will be returned and can be handled as need by your program. Enjoy!
public static int getIndexof(string name, bool ignoreCase, Type enumeration)
{
//get the names of all parts of the enum
string[] names = Enum.GetNames(enumeration);
//loop through each name
for (int i = 0; i < names.Length; i++)
{
//if ignoring cse check with lower case
if (ignoreCase)
{
if (names[i].ToLower() name.ToLower())
{
return i;
}
}
//otherwise do a direct check
else
{
if (names[i] name)
{
return i;
}
}
}
//wasn't found
return -1;
}
