use of unassigned local variable

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I am instantiating a class in a switch statement as there are a number
of different overloads depending upon the data entered by the user.

The problem I have is that after instantiating my class, when I try to
call methods in the class later on in my code using the same object I
cannot, I get the error 'use of unassigned local variable'.

How do I get around this as I need some sort of switch/if statement to
instantiate my class?


Any assistance would be really appreciated.


Cheers,

Mike
 
Hi Mike,

Maybe you have declared the variable in a local scope. Put the declaration
in the class scope - i.e. within the class definition but outside any methods.

It would help if you could post the code.

HTH,
Rakesh Rajan
 
Posting the offending code would be useful, but I guess you have simething like this:

Foo f;

switch( i )
{
case 1:
f = new Foo(42);
break;
case 2:
f = new Foo("Hello");
}

f.DoSomething(); // this causes the error

The issue is the compiler doesn't know whether any of the cases will have resulted in the creation of a Foo to assign to teh reference so it is uninitialized as far as the compiler is concerned.

So change the first line to:

Foo f = null;

This is now initialized to a known value (null) it doesn't have to be hooked up to an instance, just set to a known value.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I am instantiating a class in a switch statement as there are a number
of different overloads depending upon the data entered by the user.

The problem I have is that after instantiating my class, when I try to
call methods in the class later on in my code using the same object I
cannot, I get the error 'use of unassigned local variable'.

How do I get around this as I need some sort of switch/if statement to
instantiate my class?


Any assistance would be really appreciated.


Cheers,

Mike
 
Mike P said:
I am instantiating a class in a switch statement as there are a number
of different overloads depending upon the data entered by the user.

The problem I have is that after instantiating my class, when I try to
call methods in the class later on in my code using the same object I
cannot, I get the error 'use of unassigned local variable'.

How do I get around this as I need some sort of switch/if statement to
instantiate my class?


Any assistance would be really appreciated.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
That was exactly the problem I was having...thanks for your solution
Richard!

Cheers,
Mike
 
Hi,

You have to assign it in all possible paths of execution IIRC,
something like this:

object o;
switch ( .. )
{
case 1:
o = new object();
break;

case 2:
o = new object();
break;
case 3:
o = new object();
break;
}

if you left one case without assignation you will get the error, the same
would happen if you do

object o;
if ( var )
o = new object;

you would get the same error

Cheers,
 
Back
Top