Click here to Skip to main content
15,886,816 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Passing Enum type as a parameter

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
30 Aug 2011CPOL 7.8K  
"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...
"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.

C#
private Enum GetNextEnum(object currentlySelectedEnum)
{
	Type enumList = currentlySelectedEnum.GetType();

_______

You could have used the generic type to return a value of the correct type.

C#
private T GetNextEnum<t>(T currentlySelectedEnum)
{
	...

	return (T)enums.GetValue(index);
}</t>

License

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


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

Comments and Discussions

 
-- There are no messages in this forum --