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!
|