VB.NET Equiv of...

B

Brian W

Hi All!

Does VB.NET have an equivelent of the following C# statement (the using
part):

using (SqlConnection connection = new SqlConnection(connStr))
{
}


TIA
BW
 
T

Tim Wilson

The "using" statement will be introduced to the VB.Net language in the 2.0
release. For now you can just use a try/finally/dispose pattern, which is
essentially what the using statement gets blown out into.
 
O

Oenone

Does VB.NET have an equivelent of the following C# statement (the
using part):

using (SqlConnection connection = new SqlConnection(connStr))
{
}

I believe this would translate as:

Dim connection As New SqlConnection(connStr)
With connection
[...]
End With

Hope that helps,
 
O

Oenone

Tim said:
The "using" statement will be introduced to the VB.Net language in
the 2.0 release. For now you can just use a try/finally/dispose
pattern, which is essentially what the using statement gets blown out
into.

Ah, ok, ignore my response then. :)
 
H

Herfried K. Wagner [MVP]

Oenone said:
Does VB.NET have an equivelent of the following C# statement (the
using part):

using (SqlConnection connection = new SqlConnection(connStr))
{
}

I believe this would translate as:

Dim connection As New SqlConnection(connStr)
With connection
[...]
End With

No, it won't ;-).
 
H

Herfried K. Wagner [MVP]

Brian W said:
Does VB.NET have an equivelent of the following C# statement (the using
part):

using (SqlConnection connection = new SqlConnection(connStr))
{
}

VB.NET 2002/2003 don't provide such a construct, but VB 2005 will include
'Using' too. 'using' can be replaced by this "pattern":

\\\
Dim Connection As SqlConnection
Try
Connection = New SqlConnection()
Connection.Open()

' Use the connection here.
Finally
If Not Connection Is Nothing Then
Connection.Dispose()
End If
End Try
///
 
B

Brian W

BUZZZZZZZZ! nope!

A VB.NET With block only allows the developer to not specify the object
name when calling properties, methods etc. As in:

With myObj
.DoSomething()
.DoSomethingElse
aString = .ToString()
End With

The using statement, as representedin my example, means the compiler
will automatically call Dispose() when the assigned instance goes out
of scope.

So, the object assigned in the using statement must implement interface
IDispose.

For the C# folks, I realize the using statement is a C# construct;
however, I am just trying to find if there is an equivelent construct
in VB.NET.

Does VB.NET have an equivelent of the following C# statement (the
using part):

using (SqlConnection connection = new SqlConnection(connStr))
{
}

I believe this would translate as:

Dim connection As New SqlConnection(connStr)
With connection
[...]
End With

Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top