To those that know what their doing ... C# scope limitations question. 
Author Message
 To those that know what their doing ... C# scope limitations question.

How do I close the oDr object (see example below) if the Try statement below
triggers one of the catch statements?.

The things I've tried ...

(1) I would normally issue an oDr.Close( ) at the end of the method oDr
cannot be referenced outside of the Try block.

(2) Instantiate the data reader object just before the try block.  But new
OledbDataReader wants a connection string that isn't readily avaliable.  I
suppose I could say 'string sBogusConnectionString and then feed that to the
data reader but that seems really lame.

So how do you that know what you're doing solve this problem???

===================================================

Try
{
    OledbDataReader oDr = oOledbCmd.ExecuteReader( );

Quote:
}

Catch( ...)
{
   ...

Quote:
}

Catch(...)
{
    ...

Quote:
}

oDr.Close( );     <<< C# won't let me do this.  What are my alternatives???

=================================================



Mon, 01 Nov 2004 02:08:06 GMT  
 To those that know what their doing ... C# scope limitations question.
BBFrost, you have a couple options, (probably more than I list too)

1. declare your variable outside of the try and then initialize it inside
your try.  This way it is scoped higher than just your try{}
2. Another would be to put your oDr.Close in a finally{} block.  A finally
block will always be excuted regardless of whether an exception is thrown.

--
Greg
http://www.claritycon.com/


Quote:
> How do I close the oDr object (see example below) if the Try statement
below
> triggers one of the catch statements?.

> The things I've tried ...

> (1) I would normally issue an oDr.Close( ) at the end of the method oDr
> cannot be referenced outside of the Try block.

> (2) Instantiate the data reader object just before the try block.  But new
> OledbDataReader wants a connection string that isn't readily avaliable.  I
> suppose I could say 'string sBogusConnectionString and then feed that to
the
> data reader but that seems really lame.

> So how do you that know what you're doing solve this problem???

> ===================================================

> Try
> {
>     OledbDataReader oDr = oOledbCmd.ExecuteReader( );
> }

> Catch( ...)
> {
>    ...
> }

> Catch(...)
> {
>     ...
> }

> oDr.Close( );     <<< C# won't let me do this.  What are my
alternatives???

> =================================================



Mon, 01 Nov 2004 02:38:31 GMT  
 To those that know what their doing ... C# scope limitations question.
BBFrost,

    I would do it like this:

// Declare the instance of the data reader, but do not populate it.
OledbDataReader oDr = null;

// Get the reader.
try
{
    // Get the reader now.
    oDr = oOledbCmd.ExecuteReader();

Quote:
}

catch
{

Quote:
}

catch
{

Quote:
}

finally
{
    // If the reader is set, then close it here.
    if (oDr != null)
        // Close the reader.
        oDr.Close();

Quote:
}

    My assumption is that you always want to close the oDr instance after
the try/catch block no matter what happens.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> How do I close the oDr object (see example below) if the Try statement
below
> triggers one of the catch statements?.

> The things I've tried ...

> (1) I would normally issue an oDr.Close( ) at the end of the method oDr
> cannot be referenced outside of the Try block.

> (2) Instantiate the data reader object just before the try block.  But new
> OledbDataReader wants a connection string that isn't readily avaliable.  I
> suppose I could say 'string sBogusConnectionString and then feed that to
the
> data reader but that seems really lame.

> So how do you that know what you're doing solve this problem???

> ===================================================

> Try
> {
>     OledbDataReader oDr = oOledbCmd.ExecuteReader( );
> }

> Catch( ...)
> {
>    ...
> }

> Catch(...)
> {
>     ...
> }

> oDr.Close( );     <<< C# won't let me do this.  What are my
alternatives???

> =================================================



Mon, 01 Nov 2004 02:39:59 GMT  
 To those that know what their doing ... C# scope limitations question.
You can use a finally block. It is always executed
regardless of whether an exception is thrown.

try
{
    OledbDataReader oDr = oOledbCmd.ExecuteReader( );

Quote:
}

catch( ...)
{
 ...
Quote:
}

finally
{
 oDr.Close( );    

Quote:
}

-Mini

Quote:
>-----Original Message-----
>How do I close the oDr object (see example below) if the
Try statement below
>triggers one of the catch statements?.

>The things I've tried ...

>(1) I would normally issue an oDr.Close( ) at the end of
the method oDr
>cannot be referenced outside of the Try block.

>(2) Instantiate the data reader object just before the
try block.  But new
>OledbDataReader wants a connection string that isn't

readily avaliable.  I
Quote:
>suppose I could say 'string sBogusConnectionString and

then feed that to the
Quote:
>data reader but that seems really lame.

>So how do you that know what you're doing solve this
problem???

>===================================================

>Try
>{
>    OledbDataReader oDr = oOledbCmd.ExecuteReader( );
>}

>Catch( ...)
>{
>   ...
>}

>Catch(...)
>{
>    ...
>}

>oDr.Close( );     <<< C# won't let me do this.  What are
my alternatives???

>=================================================

>.



Mon, 01 Nov 2004 02:43:12 GMT  
 To those that know what their doing ... C# scope limitations question.
Greg,

Thanks for the quick reply!   Keep forgetting about that Finalize
statement!!

Really appreciate you taking the time to answer.

Sincerely,

Barry Frost



Quote:
> BBFrost, you have a couple options, (probably more than I list too)

> 1. declare your variable outside of the try and then initialize it inside
> your try.  This way it is scoped higher than just your try{}
> 2. Another would be to put your oDr.Close in a finally{} block.  A finally
> block will always be excuted regardless of whether an exception is thrown.

> --
> Greg
> http://www.claritycon.com/



> > How do I close the oDr object (see example below) if the Try statement
> below
> > triggers one of the catch statements?.

> > The things I've tried ...

> > (1) I would normally issue an oDr.Close( ) at the end of the method oDr
> > cannot be referenced outside of the Try block.

> > (2) Instantiate the data reader object just before the try block.  But
new
> > OledbDataReader wants a connection string that isn't readily avaliable.
I
> > suppose I could say 'string sBogusConnectionString and then feed that to
> the
> > data reader but that seems really lame.

> > So how do you that know what you're doing solve this problem???

> > ===================================================

> > Try
> > {
> >     OledbDataReader oDr = oOledbCmd.ExecuteReader( );
> > }

> > Catch( ...)
> > {
> >    ...
> > }

> > Catch(...)
> > {
> >     ...
> > }

> > oDr.Close( );     <<< C# won't let me do this.  What are my
> alternatives???

> > =================================================



Mon, 01 Nov 2004 03:20:22 GMT  
 To those that know what their doing ... C# scope limitations question.
Nicholas

Great!  It works!! Thanks!  :-)  Didn't dawn on me to instantiate to null.
Duh. Old dog, new tricks hard.

Barry



Quote:
> BBFrost,

>     I would do it like this:

> // Declare the instance of the data reader, but do not populate it.
> OledbDataReader oDr = null;

> // Get the reader.
> try
> {
>     // Get the reader now.
>     oDr = oOledbCmd.ExecuteReader();
> }
> catch
> {

> }
> catch
> {

> }
> finally
> {
>     // If the reader is set, then close it here.
>     if (oDr != null)
>         // Close the reader.
>         oDr.Close();
> }

>     My assumption is that you always want to close the oDr instance after
> the try/catch block no matter what happens.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > How do I close the oDr object (see example below) if the Try statement
> below
> > triggers one of the catch statements?.

> > The things I've tried ...

> > (1) I would normally issue an oDr.Close( ) at the end of the method oDr
> > cannot be referenced outside of the Try block.

> > (2) Instantiate the data reader object just before the try block.  But
new
> > OledbDataReader wants a connection string that isn't readily avaliable.
I
> > suppose I could say 'string sBogusConnectionString and then feed that to
> the
> > data reader but that seems really lame.

> > So how do you that know what you're doing solve this problem???

> > ===================================================

> > Try
> > {
> >     OledbDataReader oDr = oOledbCmd.ExecuteReader( );
> > }

> > Catch( ...)
> > {
> >    ...
> > }

> > Catch(...)
> > {
> >     ...
> > }

> > oDr.Close( );     <<< C# won't let me do this.  What are my
> alternatives???

> > =================================================



Mon, 01 Nov 2004 03:22:08 GMT  
 To those that know what their doing ... C# scope limitations question.
Hi Barry.

I understand your frustration.  The scope in try/catch/finally does cause
some inconvenicence as documented below. This works the same way in C/C++.
There is really no workaround beside the one you mentioned on the last
thread.

The name 'name' does not exist in the class or namespace 'namespace'

An attempt was made to use a name that does not exist in the namespace.

The following sample generates CS0103:

// CS0103.cs
using System;

class MyClass
{
   public static void Main()
   {
      // MyClass test = null;
      try
      {
         MyClass conn = new MyClass();   // delete this line
         // and uncomment the following line and the line above the try
         // conn = new MyClass();
      }
      catch(Exception e)
      {
         if (conn != null)   // CS0103
            Console.WriteLine("{0}", e);
      }
   }

Quote:
}



Mon, 01 Nov 2004 03:30:48 GMT  
 To those that know what their doing ... C# scope limitations question.
Raymond,

Thanks for the reply.  I'm slowly getting used to being a VB programmer in
the world of 'C'.  Sooner or later the neurons will 'kick in' and I'll begin
to truly 'think' in 'C' terms rather than translate in terms of my past
languages.

All I can say is that these groups (microsoft.public.dotnet) have saved my
'bacon' countless times.   My thanks to Microsoft for providing access to
these groups.   I wish other venders would, get a clue, provide the same
service.  A tremendous number of corporations and agencies do not provide
access to nntp news groups because of the administrative overhead.

Barry Frost



Quote:
> Hi Barry.

> I understand your frustration.  The scope in try/catch/finally does cause
> some inconvenicence as documented below. This works the same way in C/C++.
> There is really no workaround beside the one you mentioned on the last
> thread.

> The name 'name' does not exist in the class or namespace 'namespace'

> An attempt was made to use a name that does not exist in the namespace.

> The following sample generates CS0103:

> // CS0103.cs
> using System;

> class MyClass
> {
>    public static void Main()
>    {
>       // MyClass test = null;
>       try
>       {
>          MyClass conn = new MyClass();   // delete this line
>          // and uncomment the following line and the line above the try
>          // conn = new MyClass();
>       }
>       catch(Exception e)
>       {
>          if (conn != null)   // CS0103
>             Console.WriteLine("{0}", e);
>       }
>    }
> }



Mon, 01 Nov 2004 03:51:49 GMT  
 To those that know what their doing ... C# scope limitations question.
Thank you Barry.

As a side note. The "using" can come in handy sometime although I don't
think it applies in this case. The using statement will call the Dispose
method at the end. Although Dispose and Close are suppose to semantically
the same, but in the connection case, Dispose will ensure the connection
will go back to the connection pool.

Remarks
You create an instance in a using statement to ensure that Dispose is
called on the object when the using statement is exited. A using statement
can be exited either when the end of the using statement is reached or if,
for example, an exception is thrown and control leaves the statement block
before the end of the statement.

The object you instantiated must implement the System.IDisposable interface.

Example
// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
{
   public static void Main()
   {
      using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new
Font("Arial", 10.0f))
      {
         // use MyFont and MyFont2
      }   // compiler will call Dispose on MyFont and MyFont2

      Font MyFont3 = new Font("Arial", 10.0f);
      using (MyFont3)
      {
         // use MyFont3
      }   // compiler will call Dispose on MyFont3

   }

Quote:
}

See Also


Mon, 01 Nov 2004 05:05:19 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. scope - C# object ref question

2. WM_COPYDATA - any known limitations?

3. I know this can be done

4. Thought I knew what I was doing

5. HELP: I know this can be done better

6. Need to know when a CommitTrans is done

7. C# Limitations?

8. Scope of C#

9. no global scope in C#, but...

10. What is the common methjod of doing this in C#

11. scope of variable in child scopes

12. can this be done using C# in .NET?

 

 
Powered by phpBB® Forum Software