472,348 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Question about 'Using' and resources

mg
I'm migrating from VB6 and have a question about using 'Using' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a 'Using' block?
-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?
-Any cleaner way to do this?

Thanks!

Aug 31 '06 #1
10 1799
Mg,

As long as you use a form or things like that, than don't botter to much
about cleaning up.
(The dispose is in the component part of that)

VB.Net is a programming language inside the managed code group. That managed
code is special to overcome all cleaning troubles. Only unmanaged
*resources* are not cleaned up by the managed code.

In the system.data class that you show, there is not one piece of unmanaged
resource and therefore nothing of that has to be disposed by hand.

The using is quiet standard, it is using dispose consequently at the end.
That can be needed, but as I wrote not for the system.data classes.

Cor

"mg" <bi*****@gmail.comschreef in bericht
news:11**********************@74g2000cwt.googlegro ups.com...
I'm migrating from VB6 and have a question about using 'Using' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a 'Using' block?
-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?
-Any cleaner way to do this?

Thanks!

Aug 31 '06 #2
See inline.

mg wrote:
I'm migrating from VB6 and have a question about using 'Using' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a 'Using' block?
The 'Using' keyword will automatically generate the call to Dispose at
the end of the block. The only thing you can say for certain is that
it affects the object specified in the 'Using' expression. In your
case, however, it probably disposes any OleDbCommand and
OleDbDataReader objects associated with the OleDbConnection as well.
But, that's really an implementation detail of OleDbConnection.
-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?
I'd say probably, but it never hurts to wrap those with the 'Using'
keyword as well.
-Any cleaner way to do this?
Well, it's not clear to me what dbConx("open") does and where CN is
created. I usually create the OleDbConnection object inline with the
'Using' construct that way its scope is limited to just that block.
>
Thanks!
Aug 31 '06 #3
Cor,

See my comments inline.

Brian

Cor Ligthert [MVP] wrote:
Mg,

As long as you use a form or things like that, than don't botter to much
about cleaning up.
(The dispose is in the component part of that)

VB.Net is a programming language inside the managed code group. That managed
code is special to overcome all cleaning troubles. Only unmanaged
*resources* are not cleaned up by the managed code.

In the system.data class that you show, there is not one piece of unmanaged
resource and therefore nothing of that has to be disposed by hand.
We all know that the GC will eventually do it for us automatically when
the finalizer runs. But, the issue is with the timing of when that
happens. And I'd say it's likely that there are unmanaged resources
involved somewhere in those objects. The fact that they implement
IDisposable is a pretty good clue.
The using is quiet standard, it is using dispose consequently at the end.
That can be needed, but as I wrote not for the system.data classes.
Calling Dispose (or using the 'Using' keyword) is definitely
recommended for the classes in System.Data. If you don't then a
database connection may be held open until the GC runs and that may
take awhile.
Cor
Aug 31 '06 #4
mg
Brian Gideon wrote:
Well, it's not clear to me what dbConx("open") does and where CN is
created. I usually create the OleDbConnection object inline with the
'Using' construct that way its scope is limited to just that block.
Brian, thanks for that. CN is created in a function dbConx, just a
standard new OleDbConnection. I agree, it would be good to clearly
define its scope.

And for Cor, this example is fron a console app, not a form based app.

On a similar topic, is it better to open/close a OleDbConnection
connection as quick as possible, or is it better to create a
OleDbConnection using block around a series of OleDbCommand and
OleDbDataReader objects? By better I mean performance/resources wise.

Thanks, mg

Aug 31 '06 #5
Brian,
Calling Dispose (or using the 'Using' keyword) is definitely
recommended for the classes in System.Data. If you don't then a
database connection may be held open until the GC runs and that may
take awhile.
By whom is that recomended?

Cor

"Brian Gideon" <br*********@yahoo.comschreef in bericht
news:11*********************@m79g2000cwm.googlegro ups.com...
Cor,

See my comments inline.

Brian

Cor Ligthert [MVP] wrote:
>Mg,

As long as you use a form or things like that, than don't botter to much
about cleaning up.
(The dispose is in the component part of that)

VB.Net is a programming language inside the managed code group. That
managed
code is special to overcome all cleaning troubles. Only unmanaged
*resources* are not cleaned up by the managed code.

In the system.data class that you show, there is not one piece of
unmanaged
resource and therefore nothing of that has to be disposed by hand.

We all know that the GC will eventually do it for us automatically when
the finalizer runs. But, the issue is with the timing of when that
happens. And I'd say it's likely that there are unmanaged resources
involved somewhere in those objects. The fact that they implement
IDisposable is a pretty good clue.
>The using is quiet standard, it is using dispose consequently at the end.
That can be needed, but as I wrote not for the system.data classes.

Calling Dispose (or using the 'Using' keyword) is definitely
recommended for the classes in System.Data. If you don't then a
database connection may be held open until the GC runs and that may
take awhile.
>Cor

Sep 1 '06 #6
Cor,

In general, the author of the class recommends it when he/she derives
from IDisposable. So I guess that means Microsoft recommends it. One
notable exception is the DataSet. IDisposable is carried along with
MarshalByValueComponent, but we're all pretty sure that a DataSet does
not hold unmanaged resources. But, that's not what this thread is
about. We're talking about OleDbConnection. If you choose not to call
Dispose or Close then the connection will remain open which may lead to
a bug. It's easy to demonstrate problems by creating a connection
object and calling Open in a loop repeatedly without closing or
disposing the object.

Brian

Cor Ligthert [MVP] wrote:
Brian,
Calling Dispose (or using the 'Using' keyword) is definitely
recommended for the classes in System.Data. If you don't then a
database connection may be held open until the GC runs and that may
take awhile.

By whom is that recomended?

Cor
Sep 1 '06 #7
Brian,

Who is that author of the class, I am almost sure not the leader of the
ADONET group, as far as I remember me he has written many times that closing
is more than enough for a connection in the ADONET newsgroep.

Cor

"Brian Gideon" <br*********@yahoo.comschreef in bericht
news:11**********************@m73g2000cwd.googlegr oups.com...
Cor,

In general, the author of the class recommends it when he/she derives
from IDisposable. So I guess that means Microsoft recommends it. One
notable exception is the DataSet. IDisposable is carried along with
MarshalByValueComponent, but we're all pretty sure that a DataSet does
not hold unmanaged resources. But, that's not what this thread is
about. We're talking about OleDbConnection. If you choose not to call
Dispose or Close then the connection will remain open which may lead to
a bug. It's easy to demonstrate problems by creating a connection
object and calling Open in a loop repeatedly without closing or
disposing the object.

Brian

Cor Ligthert [MVP] wrote:
>Brian,
Calling Dispose (or using the 'Using' keyword) is definitely
recommended for the classes in System.Data. If you don't then a
database connection may be held open until the GC runs and that may
take awhile.

By whom is that recomended?

Cor

Sep 1 '06 #8
mg,
| I'm migrating from VB6 and have a question about using 'Using' and the
| best way to use it.
Using "Using" is really about when to call Dispose.

When to call Dispose?

* Call Dispose when the type itself implements IDisposable

* Call Dispose when the type or one of its base classes *overrides*
Dispose(Boolean) if the class inherits from System.ComponentModel.Component
or System.ComponentModel.MarshalByValueComponent

* Do not explicitly call Dispose on classes deriving from
System.Windows.Forms.Control for instances placed on a
System.Windows.Forms.Form as Form will implicitly dispose of them when the
form is Disposed

* Call Dispose on System.Windows.Forms.Form objects when Form.ShowDialog is
used.

* Do not explicitly call Dispose on System.Windows.Forms.Form objects if
Form.Show is used as Dispose will be implicitly called when the form is
closed

* Do not explicitly call Dispose on classes deriving from
System.Web.UI.Control as it will be implicitly called as part of the normal
ASP.NET page processing

I consider the second rule controversial as it relies on using ILDASM or
Reflector to find out implementation details of a class. Its meant for
classes such as DataSet, that have an inherited Dispose, but Dispose doesn't
really do anything.

These rules are based on private discussions with other MVPs & discussions
held in the newsgroups earlier in 2005.

These rules apply to objects that you create, explicitly or implicitly.
Objects that you "own".

Disposable Objects that are passed to you as a parameter of a method (such
as the Graphics object on the Paint event) should not have their disposed
method call, as the system calls it as part of the method that raises the
event.

Objects that something else "owns" should normally be disposed of by the
"owning" object.

Based on the above rules I would rewrite your code snippet as:

Using CN As OleDbConnection = dbConx("open")
Using CMD As New OleDbCommand(sSQL, CN)
Using DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
Else
Return False
End If
End Using
End Using
End Using

Or alternatively:

Using CN As OleDbConnection = dbConx("open"), CMD As New
OleDbCommand(sSQL, CN), DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
Else
Return False
End If
End Using

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"mg" <bi*****@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
| I'm migrating from VB6 and have a question about using 'Using' and the
| best way to use it.
|
| Here is a example of a small bit of code:
|
| dbConx("open")
| Using CN
| Dim CMD As New OleDbCommand(sSQL, CN)
| Dim DR As OleDbDataReader = CMD.ExecuteReader()
|
| If DR.HasRows = True Then
| Dim vCID As New Collection()
| While DR.Read()
| vCID.Add(DR.GetInt32(0))
| End While
| DR.Close()
| CMD.Dispose()
| dbConx("close")
| Else
| DR.Close()
| CMD.Dispose()
| dbConx("close")
| Return False
| End If
| End Using
|
| dbConx creates an OleDbConnection CN and opens/closes it. The purpose
| of this is to return 0 or more record numbers for later use. If zero
| records are returned I just exit the function with a return of false.
| If records found, gather them into a collection and carry on, closing
| all resources used.
|
| In VB6 I used "set x=nothing" to clean-up.
|
| -What gets cleaned up within a 'Using' block?
| -Does my CMD and DR objects get cleaned up along with CN (so not
| needing all the .close and .dispose lines?
| -Any cleaner way to do this?
|
| Thanks!
|
Sep 1 '06 #9
mg
Jay thanks, you are a star! I don't mind the back-and-forth between
the alpha-devs, but was really looking for the practical, not the
theory, and you came through.

For me, my recent jump to .net from VB6 has moved me way out of my
comfort zone. I just have to get used to not being so productive for a
while...

Thanks again!
mg

Sep 1 '06 #10

Cor Ligthert [MVP] wrote:
Brian,

Who is that author of the class, I am almost sure not the leader of the
ADONET group, as far as I remember me he has written many times that closing
is more than enough for a connection in the ADONET newsgroep.

Cor
Cor,

Well yeah, that's what I've been saying. Dispose *will* close the
connection. Whether you choose to call Close or Dispose it's all the
same. The important thing to consider is that IDisposable is your clue
that it needs to be done.

Brian

Sep 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tony Johansson | last post by:
Hello experts!! I reading in a book about C++ and there is something that I'm not sure about. I don't belive that the book is wrong but I will...
62
by: ROSY | last post by:
hello experts plz answer following questions::: thanks in advance. 1. Out of fgets() and gets() which function is safe to use and why? 2. What is...
21
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText,...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and...
1
by: scpedicini | last post by:
Let's say that I've built an assembly, called myapi.dll whose default resource messages are english. Then let's say I create a german satellite...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are...
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information....
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2)...
0
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.