Dispose and 'With' blocks

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi, I think I remember reading somewhere that if you use a With block as
below, then the call to Dispose in the Finally block is completely
redundant because Dispose will be called by the system when the End With
is reached. Can anyone confirm this, or otherwise?

With New SqlCommand()
Try
.CommandType = CommandType.StoredProcedure
.CommandText = "dbo.uspCRChangeRequestInsert"
'Irrelevant code missing here...
.Connection = Conn
Conn.Open()
.ExecuteScalar()
Catch Ex As Exception
'Handle error
Finally
.Dispose() 'Is this line completely redundant??
Conn.Dispose()
End Try
End With

(Now I know that Dispose is never strictly required for managed
components, but we call it to ensure all connections are returned to the
pool as quickly as possible.)

Thanks,
Paul
 
With does not equate to the c# using. It is just a convenience.

However, you do not need to dispose SqlCommand or SqlConnection objects. All
that is required is that you close all your connections when you are done
with them.
 
using?


Marina said:
With does not equate to the c# using. It is just a convenience.

However, you do not need to dispose SqlCommand or SqlConnection objects. All
that is required is that you close all your connections when you are done
with them.
 
Yes, c# has a 'using' keyword that does what you were asking about. You can
look it up, it's well documented.
 
Wow. Learn something new every day. =)


Marina said:
Yes, c# has a 'using' keyword that does what you were asking about. You can
look it up, it's well documented.

block
 
Actually it was me that asked the original question, but now you mention
it I think it was the C# 'using' keyword I was thinking of. We have to
use VB .NET at work, but I did look into C# a while back which must have
been where I got this confusion from!

My thanks to yourself and CJ Taylor,

Paul
 
Actually it was me that asked the original question, but now you mention
it I think it was the C# 'using' keyword I was thinking of. We have to

You may be interested to know that the next version of VB.Net (Whidbey)
will have a Using block that works like the C# using statement. Here's an
example:

Using theFont As New Font("Arial", 10.0F)
txtFontA.Font = theFont
txtFontA.Text = "Hello there"
End Using

When the code hits the End Using block, the font is automatically disposed.
The font will be disposed even if an exception is thrown inside the Using
block.
 
* Chris Dunaway said:
You may be interested to know that the next version of VB.Net (Whidbey)
will have a Using block that works like the C# using statement. Here's an
example:

Using theFont As New Font("Arial", 10.0F)
txtFontA.Font = theFont
txtFontA.Text = "Hello there"
End Using

When the code hits the End Using block, the font is automatically disposed.
The font will be disposed even if an exception is thrown inside the Using
block.

Right. But for some reason, they missed to include a really useful
'Using With' block.
 
Right. But for some reason, they missed to include a really useful
'Using With' block.

I'm not sure I understand what you intend. Do you mean a Using With on an
existing object? How would Using With be different than just Using?

Do you mean something like this? :


'\\\\
Dim obj As New SomeDisposableObject

Using obj
'Do stuff with the obj
End Using

'In theory, obj should be disposed here.
'////

This code seems to work. What do you mean by "Using With"?

Chris
 
Hi Herfried,

Are you meaning this

Using theFont As New Font("Arial", 10.0F)
With txtFontA
.Font = theFont
.Text = "Hello there"
End With
End Using

Funny code.

Cor
 
Right. But for some reason, they missed to include a really useful
'Using With' block.

I tried the following code:

Using New DisposableClass("something")
'Cant work with new object here? How to reference it?
End Using

It still creates the object and still calls the dispose, but without a
variable name, I cannot access the properties of the object. A simple
period doesn't do the trick.

Is that what you mean?
 
* Chris Dunaway said:
I'm not sure I understand what you intend. Do you mean a Using With on an
existing object? How would Using With be different than just Using?

Sample:

\\\
Using With New Person()
.Name = "Peter"
.Age = 22
 
* Chris Dunaway said:
I tried the following code:

Using New DisposableClass("something")
'Cant work with new object here? How to reference it?
End Using

It still creates the object and still calls the dispose, but without a
variable name, I cannot access the properties of the object. A simple
period doesn't do the trick.

Is that what you mean?

Partly.

I would prefer:

\\\
Using With New DisposableClass("something")
.Foo()
.Bar()
.Baz = 22
.Goo()
End Using
///
 
Back
Top