Where’s my enum?

March 30, 2007

A teammate yesterday came to me and asked me about a bug that caused a crash in the application. It appears originated from the exceptions class that I just built. The funny thing is, upon tracing the code, the CLR throws an TypeLoad Exception whenever a particular exception is built. Looking deeper at the class constructor, points out the exact place where the application falls. The code is a simple code that assigns a private member to a specific enum value. Simple, right? No chance of throwing an exception, right!? Wrong! This is weird, I thought. Looking at the exception type, I realized that the reason is that the enumeration type couldn’t be determined, or rather ‘Not Found’ in the current assembly. I wonder if it’s true… so I fired ILDASM and looked at the IL. And,.. the corresponding enum didn’t get registered in the assembly. ??!?”#%$&&!!. Looking at the code, I quickly traced my program and found out that I declared the enum inside a class. I cut the enum and pasted it outside the class, specifying the access modifier to public and, voila!. The problem was fixed.

Lesson learned: It’s safer to declare your enums outside the class, coz subclass inherits parent class access modifier (which, in my case, was a private class). Upon experimenting with enums, I found out that public class with public public member variable access modifier is the only way you can expose the enums outside and get it compiled into the assembly. My guess is, since enums basically just ‘defines’, when it’s not exposed outside (externally, so that it can be used by other assemblies), the compiler just optimizes it all, and eliminates the enums from the assembly. But, becoz IL still refer to it as a value from enum class, it couldn’t locate the type. Just a wild guess.. I might be wrong.  BTW, I’m on Visual C++ .Net 2003.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: