Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Alternatives
 

Passing Enum type as a parameter

I think the start is wrong... we can pass types as parameters.A method like this:void ShowName(Type type){ Console.WriteLine(type.FullName);}can be called as:ShowName(typeof(object));or as this:ShowName(someObject.GetType());If you are trying to say that we can't...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

30 Aug 2011
jarvisa
"we cannot pass a Type as a function parameter" ???Yes we can. Just look at the parameter of Enum.GetValues for example._______Generic functions are unnecessary here. Just use Object.GetType to, um, get the type.private Enum GetNextEnum(object currentlySelectedEnum){ Type...
23 Aug 2011
Ed Hellyer Evosis LLC
5 alternatives  
Cycling through (looping around) enum lists.
24 Aug 2011
Daniel Gidman
I think the compile time force for a little enum enforcement would look something like this... (excuse the bad syntax, doing it from memory)private T GetPreviousEnum(this T currentlySelectedEnum) where T: structStackOverflow has a good question and answer in relation to it here.[^].
30 Aug 2011
Narf the Mouse
Seeing as I "researched" your code and the code on StackOverflow for my own implementations, I figure I should post this - It's a default enumerator for any Enum. Free to use, free to copy; just attribute alike :) : /// /// Created based on the "Passing Enum type...
6 Sep 2011
funazonki
Perhaps I'm missing something here, but why not juststatic IEnumerable GetEnums(){ Type t = typeof (T); return from object value in Enum.GetValues(t) select (T)Enum.ToObject(t, value);}

License

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

About the Author

Paulo Zemek
Architect
Canada Canada
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
 
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.
 
Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
 
Codeproject MVP 2012
Microsoft MVP 2013

| | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid