You can't initialise variables inside a try..catch block and then use them
outside of that block.  Simply because not everything within try{} or
catch{} is guaranteed to be executed, so when you reach the other side of it
(your 'return author' line), that variable might not have been assigned.
Initialise it before the try{} block and you should be fine.

Jim

> -----Original Message-----
> From: Curtis Koppang [mailto:[EMAIL PROTECTED]]
> Sent: 19 April 2002 15:01
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Use of unassigned local variable
>
>
> I get the "use of unassigned local variable 'author'" error on the
> following line of code...
>
>     try
>     {
>          cn.Open();
>          dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
>          author = new Author(dr);
>     }
>     finally
>     {
>   cn.Close();
>     }
>
>     return author;
>
> But not on the following code.
>
>     cn.Open();
>     dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
>     author = new Author(dr);
>     cn.Close();
>
>     return author;
>
> Why would the two be considered different?
>
> Tx,
> curt
>
> You can read messages from the DOTNET archive, unsubscribe
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to