Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Tip/Trick

Passing Enum type as a parameter

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
23 Aug 2011CPOL1 min read 99.5K   6   2
Cycling through (looping around) enum lists.

In a regular method call, we cannot pass a Type as a function parameter and to be honest, my need to do this has been quite rare. However, it just so happened that the other day I needed to do just this.


In my application, I wanted the user to be able to select next or previous to iterate through a list. When we get to the end of a sequence, I want to loop around by going to the other end of the sequence.


So no big deal here until I decided that I wanted to continue to use my already established enums regardless of their underlying type. But the thing about an Enum is that it is a Type, which presented the first problem. How do I send the message about the enum I want to iterate through to a method?


Well, turns out, this is a perfect job for a generic method. With a generic method, I can send a Type as a parameter.


GetNextEnum<T> and GetPreviousEnum<T> are generic functions that can be used to cycle through any enum list of any type. In my case, the user selects “Next” or “Previous” for cycling through the CameraActionModes. The CameraActionMode are defined as an enum. I also had the same type of action needed for the MouseMode enum. Reuse baby! It is a good thing.


C#
private void SelectNextCameraMode()
{
    World.Camera.CameraMode = 
     (CameraActionMode)GetNextEnum<cameraactionmode>(World.Camera.CameraMode);
    UpdateCameraModeLabel();
}

private void SelectNextMouseMode()
{
    this.mouseMode = (MouseMode)GetNextEnum<mousemode>(this.mouseMode);
    UpdateMouseModeLabel();
}

private Enum GetNextEnum<t>(object currentlySelectedEnum)
{
    Type enumList = typeof(T);
    if (!enumList.IsEnum)
        throw new InvalidOperationException("Object is not an Enum.");

    Array enums = Enum.GetValues(enumList);
    int index = Array.IndexOf(enums, currentlySelectedEnum);
    index = (index + 1) % enums.Length;
    return (Enum)enums.GetValue(index);
}

private Enum GetPreviousEnum<t>(object currentlySelectedEnum)
{
    Type enumList = typeof(T);
    if (!enumList.IsEnum)
        throw new InvalidOperationException("Object is not an Enum.");

    Array enums = Enum.GetValues(enumList);
    int index = Array.IndexOf(enums, currentlySelectedEnum);
    index = (((index == 0) ? enums.Length : index) - 1);
    return (Enum)enums.GetValue(index);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI think the start is wrong... we can pass types as parameter... Pin
Paulo Zemek5-Sep-11 15:24
mvaPaulo Zemek5-Sep-11 15:24 
I think the start is wrong... we can pass types as parameters.
A method like this:
<pre lang="cs">
void ShowName(Type type)
{
Console.WriteLine(type.FullName);
}
</pre>

Can be called as:
<pre lang="cs">
ShowName(typeof(object));
</pre>

Or as this:
<pre lang="cs">
ShowName(someObject.GetType());
</pre>

If you are trying to say that we can't pass enum types as type parameters, I will partially agree, as we can't use where T: enum. But, you say that generic methods solve the problem, so that's not the case.

The only advantage (if that's really to be considered one) about using generic methods to pass types is that null or open generic types (like List<> without specifying a data type) will never be received. But doing the necessary ifs are probably faster than counting with the generation of the method.
GeneralReason for my vote of 3 Interesting... kind of obscure, but ... Pin
BrianBissell30-Aug-11 3:18
BrianBissell30-Aug-11 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.