Click here to Skip to main content
11,438,007 members (44,013 online)
Click here to Skip to main content

Tagged as

Passing Enum type as a parameter

, 23 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Ed Hellyer Evosis LLC

United States United States
No Biography provided

Comments and Discussions

 
GeneralI think the start is wrong... we can pass types as parameter... Pin
Paulo Zemek5-Sep-11 16:24
mvpPaulo Zemek5-Sep-11 16:24 
GeneralReason for my vote of 3 Interesting... kind of obscure, but ... Pin
BrianBissell30-Aug-11 4:18
memberBrianBissell30-Aug-11 4:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 23 Aug 2011
Article Copyright 2011 by Ed Hellyer Evosis LLC
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid