Can you post a compiling snippet, because I cannot reproduce the
behaviour you see.

The following compiles just fine:

using System;
using System.Data;

public class Test
{
        public static void Main(string[] args)
        {
        }

        public static Test Method()
        {
                Test t;
                IDbConnection conn = new
System.Data.SqlClient.SqlConnection();
                try
                {
                        conn.Open();
                        t = new Test();
                }
                finally
                {
                        conn.Dispose();
                }
                return t;
        }
}

Regards,
Jeroen

> -----Original Message-----
> From: dotnet discussion [mailto:[EMAIL PROTECTED]]
> On Behalf Of Curtis Koppang
> Sent: Friday, April 19, 2002 17: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.
>

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