Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27346

Quick OOP question - object lifetime

$
0
0
I've written a SQL Query class so that I can get query results in as few lines as possible throughout my application.
My Query class has a DataTable property (called Results) so I can access the returned rowset after the query is executed in the New(SQLString as String) method.
I've given it a load of 'Implements IDisposable' boilerplate I found online - so that I can use 'Using.. End Using' and all seems well. It's working.

Can I just ask for a quick confirmation ( I think I'm getting the hang of OOP, but want to make sure) ... given the following code...

Code:

        Dim copyDt As New DataTable
        Using test1 As New SQLstuff.Query("SELECT * from MyTable")
            MsgBox(test1.RowCount & "  " & test1.Results.Columns(0).ColumnName)
            copyDt = test1.Results  ' both are DataTable object instances
        End Using
        MsgBox(copyDt.Rows.Count & "  " & copyDt.Columns(0).ColumnName)

The two MsgBoxes give the same results.

Q) Does copyDt end up referencing the same object instance in memory as the original test1.results, and it persists after the 'Using.. End Using' block, even when test1 no longer exists, purely because the GC knows that copyDt is also pointing to the same data, and the whole lot will be GCollected and cleared after copyDt falls out of scope?

(Optional second Question - I've run a query within a FOR loop for thousands of times (impressively quick!) and in Task Manager the amount of memory used by VB stays around the same, is that a valid way to check for memory leaks?)

Thanks

Viewing all articles
Browse latest Browse all 27346

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>