Re: use of unassigned local variable
From: Richard Blewett [DevelopMentor] (richardb_at_develop.com)
Date: 10/19/04
- Next message: Lau: "Configuration Management Application Block and Compact Framework"
- Previous message: DerekS: "Re: Create System DSN for SQL 2000 DB on remote box"
- In reply to: Mike P: "use of unassigned local variable"
- Next in thread: Mike P: "Re: use of unassigned local variable"
- Reply: Mike P: "Re: use of unassigned local variable"
- Messages sorted by: [ date ] [ thread ]
To: microsoft.public.dotnet.languages.csharp Date: Tue, 19 Oct 2004 05:31:46 -0700
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
- Next message: Lau: "Configuration Management Application Block and Compact Framework"
- Previous message: DerekS: "Re: Create System DSN for SQL 2000 DB on remote box"
- In reply to: Mike P: "use of unassigned local variable"
- Next in thread: Mike P: "Re: use of unassigned local variable"
- Reply: Mike P: "Re: use of unassigned local variable"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|