But you *are* going to execute the return.

-----Original Message-----
From: Curtis Koppang [mailto:[EMAIL PROTECTED]]
Sent: 19 April 2002 16:10
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Use of unassigned local variable


I guess it's too much to ask for the compiler to figure this out.

Here is what I mean.  If I get an exception on a line in the try block, the
only code I am executing is the code in the finally block.  I am not going
to execute the return, so why do I care if it has a valid value or not.

Later,
curt

----- Original Message -----
From: "Jim Arnold" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 19, 2002 10:32 AM
Subject: Re: [DOTNET] Use of unassigned local variable


> 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.
>
>

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


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

**********************************************************************

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