Technical Journal

Programming, software technologies, operating systems, IT

Enum to List

I’ve found this method useful lately so I thought I should add it here where everyone can see it.
It is meant to be used with the .NET Compact Framework. In the full framework this would have been much easier -
without reflection.
What the method does is that it adds the enum’s fields to a generic list of the same type T as the enum.

public static List EnumToList( )
{
Type enumType = typeof( T );

if( enumType.BaseType != typeof( Enum ) )
{
throw new ArgumentException( “Not an enum type!” );
}

List enumFieldList = new List( );

FieldInfo[ ] enumFields = typeof( T ).GetFields( );

foreach( FieldInfo fieldInfo in enumFields )
{
if( !fieldInfo.IsSpecialName )
{
enumFieldList.Add( (T) Enum.Parse( enumType, fieldInfo.Name, false ) );
}
}

return enumFieldList;
}

April 24, 2009 Posted by | Windows CE | , , , , | Leave a Comment

C++ UNREFERENCED_PARAMETER

I am currently developing a driver for one of our custom made devices under Windows CE and I came across this macro -

UNREFERENCED_PARAMETER in one of the driver examples I took as reference for developing my own. The definition itself does not say much:

#define  UNREFERENCED_PARAMETER(P) (P)

All it does is to layout the parameter or expression it has been passed. After some browsing on the internet I’ve found this post by Paul DiLascia:

http://msdn.microsoft.com/en-us/magazine/cc163805.aspx which I think greatly explains what is meant by compiling with Warning Level 4 – it made me a believer anyway.

Indeed, an unreferenced parameter would cause an error when compiling using level 4 and this is where UNREFERENCED_PARAMETER comes in handy.

For instance you have a function like this:

void foo( int arg1, float arg2 )

{

     UNREFERENCED_PARAMETER( arg1 );

}

Since you won’t be using arg1 and also you will compile with level 4, you just fool the compiler by passing arg1 as parameter to a macro that just adds the line:

arg1;

to your code. And since it is not used the compiler does not generate code for it and no efficiency or space is lost.

I will not repeat here stuff written in that post, just thought I’d mention it as a great post on C++ I’ve found.

April 7, 2009 Posted by | Programming | , | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.