Home All Groups Group Topic Archive Search About
Author
12 Apr 2005 4:18 PM
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

--
-- Formerly brianw@gold_death_2_spam_rush.com

Author
12 Apr 2005 4:29 PM
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.

--
Tim Wilson
..Net Compact Framework MVP

Show quoteHide quote
"Brian W" <non***@yourbusiness.com> wrote in message
news:OvkIGt3PFHA.2136@TK2MSFTNGP14.phx.gbl...
> Hi All!
>
> Does VB.NET have an equivelent of the following C# statement (the using
> part):
>
> using (SqlConnection connection = new SqlConnection(connStr))
> {
> }
>
>
> TIA
> BW
>
> --
> -- Formerly brianw@gold_death_2_spam_rush.com
Author
12 Apr 2005 4:39 PM
Oenone
Tim Wilson wrote:
> 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. :)

--

(O)enone
Author
12 Apr 2005 4:38 PM
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)enone
Author
12 Apr 2005 5:05 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Oenone" <oen***@nowhere.com> schrieb:
>> 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 ;-).

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 Apr 2005 5:19 PM
Cor Ligthert
Herfried,

> No, it won't ;-).


Read the tread, that is what Oenone said in the answer to Tim Wilson who
gave the right answer.

Cor
Author
12 Apr 2005 5:12 PM
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.


Oenone wrote:

Show quoteHide quote
> > 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,
Author
12 Apr 2005 5:06 PM
Herfried K. Wagner [MVP]
"Brian W" <non***@yourbusiness.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>