Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Assigning an Enum to a local variable.

Hello, I have a Enum that contains 4 elements, 

enum myEnum { North, South, East, West };

Open in new window

I want to be able to test for a "location" in an IF statement as such.

myEnum location;
if(A == a)
{
   location = myEnum.North;
}
dictionary.Add(name, location). <<<< ERROR  "Use of unassigned local variable 'location'" 

Open in new window

So, how does one assign an enum to a local variable?  


Thank you,

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gr8gonzo got in just before me with the correct reason.  ,-)

ps.  It happens with other things as well, it isn't just enum values.
eg.
int i;
if(A == a) i = 42;
MyVar = i;  <---same error would happen
Avatar of Mr_Fulano

ASKER

Thank you Gentlemen, I had done the same thing, but then thought NO, that can't be right...!!!  There has to be a better way, I told myself... I guess not HA...!

Thank you, that is what I did and it worked well.
Gr8gonzo, one last comment about what you mentioned here below:

The compiler just doesn't like the idea that if (A == a) is NOT true, that location will not have a value at all.

OK, assuming that is correct, then why would the compiler care... in an IF statement if (A == a) was FALSE, the IF statement simply wouldn't fire, so why would the complier care?

I thought it was because Enums are compiled at Runtime and if the variable, which in this case is an Enum is left unassigned, then the compiler would care... it has a variable that it has no idea what to do with it prior to Runtime.

Your thoughts....

> Enums are compiled at runtime...


....eeehhhhhhh.... kind of. I wouldn't really use the term "compiled" in this situation. An enum is essentially an integer with a fancy little wrapper around it. As Andy pointed out, this particular problem is not specific to enums, though. The problem is simply the potential scenario where code is told to use a variable that MIGHT not have any value, not even a null.


string x;
Console.WriteLine(x);

Open in new window


So there are two parts to why this is a problem.


First of all, .NET does not allocate memory for variables in advance - it allocates memory as the code executes and the code hits a line that assigns a value to a variable. 


string x;     // Memory not allocated
x = "Hello!"; // Memory allocated

Open in new window


Part of the reason for this is because .NET doesn't always know how much memory the variable will need. Sure, an int will always be 4 bytes, but a string could be 1 byte or a million bytes - you don't know until the assignment.


That said, Microsoft -could- have probably set it up so that the compiler simply allowed it and would throw a fatal error at runtime if it ever tried to use a variable without a value. Instead, they figured that it is always bad code to have a situation where a variable might be used without being assigned, so they just made the compiler prevent you from doing that, which helps you avoid unexpected errors.

It's also worth noting that this is specific to variables inside of code blocks like methods ("local variables"). Things like class fields and properties are assigned their default values because when you create an instance of the class, you're creating the whole structure. So:


class Foo

{

  public int x;

  public string y;

}


...when you create an instance of Foo, that cascades into creating the whole structure in memory, and it starts with the type defaults unless you've specified your own default values.

Hi Gr8gonzo, VERY good explanations. I see your point. And yes, I understand that Enums are integers under the hood, but your explanation makes it a bit more clear.  

This was a good discussion and one I hope helps others here in the future.

Thank you.