Results 1 to 9 of 9

Thread: [RESOLVED] [2005] USING statement and multiple resources

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [RESOLVED] [2005] USING statement and multiple resources

    Hi all,

    I'd like to use one Using statement with multiple resources. MSDN says that one Using block can be used for multiple resources, however, I don't see any examples and I can't seem to figure out the syntax.

    I have already acquired the recources and would like to do something like:

    vb Code:
    1. Using conn, da
    2. ' Do something.
    3. End Using

    Thanks for any help.

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] USING statement and multiple resources

    An example is below, the resources can be of different types;

    Code:
            Using g As Graphics = e.Graphics, p As New Pen(Color.Black)
                g.DrawLine(p, Me.ClientRectangle.X, Me.ClientRectangle.Top, _
                Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
            End Using

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] USING statement and multiple resources

    Thank you. That works when I am acquiring the resources in the Using statement but what about if I have them already? This doesn't seem to to work:

    vb Code:
    1. Dim g as Graphics = e.graphics
    2.         Dim p As New Pen(Color.Black)
    3.  
    4.         Using g, p
    5.             g.DrawLine(p, Me.ClientRectangle.X, Me.ClientRectangle.Top, _
    6.             Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
    7.         End Using
    Last edited by nmadd; Jun 14th, 2007 at 04:16 PM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] USING statement and multiple resources

    The purpose of the Using/End Using it to create temporary objects etc. that are created, used and then discarded. After the "Using" part, you declare the temporary objects you want to create, you then use them and discard them by virtue of the "End Using" statement.

    So putting that new 'pen' into a Using statement is not valid, since it already exists.

  5. #5

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] USING statement and multiple resources

    Thanks for the quick reply. I did read up about the purpose of the Using statement and thought I understood it.

    MSDN says that you can pass it a variable instead of defining the resource in the Using statement itself.

    You're right that it does make sense to do it right in the statement, but I was just curious about using variables instead. I thought it would possibly make it more readable.

    Am I misunderstanding this?
    Last edited by nmadd; Jun 14th, 2007 at 04:04 PM.

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] USING statement and multiple resources

    I understand what you mean, you can do it like this;

    Code:
        Dim a As Label
    
        Private Sub Form1_Paint( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles MyBase.Paint
    
            Using g As Graphics = e.Graphics, p As New Pen(Color.Black), a = New Label
                g.DrawLine(p, Me.ClientRectangle.X, Me.ClientRectangle.Top, Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
            End Using
    
        End Sub
    The difference here is that you declare "a" as a label, but the Dim statement does not create a label. It only creates an identifier "a" which is of type "label". It is in the Using statement, that you actually create a label object.

    To be precise, "a 'Using' resource variable must have an explicit initialization".
    Last edited by Bulldog; Jun 14th, 2007 at 04:10 PM.

  7. #7

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] USING statement and multiple resources

    Thanks again.

    So, for example, something like this is just not going to work?

    vb Code:
    1. Dim conn As New OleDb.OleDbConnection(someConnectionString)
    2. Dim da As New OleDb.OleDbDataAdapter(SQLString, conn)
    3.  
    4. Using conn, da
    5.     'Fill a datatable
    6. End Using

    However, this does seem to work:

    vb Code:
    1. Using conn
    2.     Using da
    3.         'Fill a table
    4.     End Using
    5. End Using

    I'm confused as to why so I guess I'll just start defining the resources right in the Using statement since I know that works.

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] USING statement and multiple resources

    Well not really, since in your first example you have gone back to the situation where conn and da exist before the "Using" statement and hence they will still exist afterwards. Compare the two subs below;

    Code:
        Dim a As Label
        Dim b As Panel
    
        Private Sub MySub1()
            a = New Label
            b = New Panel
            Using a
                Using b
    
                End Using
            End Using
            '"a" and "b" will still exist at this point
        End Sub
    
        Private Sub MySub2()
            Using a = New Label, b = New Panel
    
            End Using
            '"a" and "b" will not exist at this point
        End Sub
    In MySub1 the objects that have already been created are used and not disposed of. In MySub2 the two objects are created and disposed.

  9. #9

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] USING statement and multiple resources

    Aha! Understood now. I thought that the Using statement would dispose of the resources regardless of where I acquired them. Thank you very much for the posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width