|
-
Jun 14th, 2007, 03:03 PM
#1
Thread Starter
Registered User
[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:
Using conn, da
' Do something.
End Using
Thanks for any help.
-
Jun 14th, 2007, 03:15 PM
#2
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
-
Jun 14th, 2007, 03:32 PM
#3
Thread Starter
Registered User
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:
Dim g as Graphics = e.graphics
Dim p As New Pen(Color.Black)
Using g, p
g.DrawLine(p, Me.ClientRectangle.X, Me.ClientRectangle.Top, _
Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
End Using
Last edited by nmadd; Jun 14th, 2007 at 04:16 PM.
-
Jun 14th, 2007, 03:44 PM
#4
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.
-
Jun 14th, 2007, 04:00 PM
#5
Thread Starter
Registered User
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.
-
Jun 14th, 2007, 04:05 PM
#6
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.
-
Jun 14th, 2007, 04:13 PM
#7
Thread Starter
Registered User
Re: [2005] USING statement and multiple resources
Thanks again.
So, for example, something like this is just not going to work?
vb Code:
Dim conn As New OleDb.OleDbConnection(someConnectionString)
Dim da As New OleDb.OleDbDataAdapter(SQLString, conn)
Using conn, da
'Fill a datatable
End Using
However, this does seem to work:
vb Code:
Using conn
Using da
'Fill a table
End Using
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.
-
Jun 14th, 2007, 04:35 PM
#8
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.
-
Jun 14th, 2007, 04:46 PM
#9
Thread Starter
Registered User
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|