Understanding passing, using and destroying objects

  • Thread starter Thread starter Miguel
  • Start date Start date
M

Miguel

Hi,
I'm new to .NET and am using VB .NET as I am from a VB background.

I am having difficulty understanding the way .NET handles, passes and uses
objects.
I have had a look at the Micrsoft Data Access Application blocks for .NET
and am struggling to understand the following:

1. When creating a function that returns a datareader, there is a datareader
instantiated inside the function. This datareader is then returned to the
calling function. What happens then to the datareader inside the function,
is this merely a pointer that the caller then uses or is the entire object
copied into a new datareader variable? Then, what happens to the datareader
inside the object, does it get destroyed??

In the example below I see 2 DataReader objects, one is called
'ExecuteReader' the other 'drReader, is drReader a pointer that then gets
assigned to 'ExecuteReader' and then passed back to the calling function???

Public Overloads Shared Function ExecuteReader(ByVal connectionString As
String, _
ByVal commandType As
CommandType, _
ByVal commandText As
String) As SqlDataReader
' Pass through the call providing null for the set of SqlParameters
Dim drReader as SQLDataReader
drReader = ExecuteReader(connectionString, commandType, commandText,
CType(Nothing, SqlParameter()))
Return drReader
End Function ' ExecuteReader


2. I have a function as listed below, it takes a datareader as an input and
processes this reader then returns an array. What I would like to know is
what happens to the datareader inside the function, must I destroy it, and
how does it get instantiated???

'
' This function takes a data reader, extracts the field details and
' places the data inside an array. It then returns the array.
'
Function TranslateData(ByVal drReader As SqlDataReader) As String(,)
'
' First dimension will hold the field names,
' second dimension will hold the value
'
Dim sRetVal(drReader.FieldCount - 1, 1) As String
Dim schemaTable As DataTable = drReader.GetSchemaTable()
Dim myRow As DataRow
Dim myCol As DataColumn
Dim iRow As Integer = 0
'
' Traverse the fields in the reader
'
For Each myRow In schemaTable.Rows
'
' Retrieve the appropriate column
'
myCol = schemaTable.Columns(1)
'
' Insert the column name into the array
'
sRetVal(iRow, 0) = myRow(myCol).ToString
'
' Insert the actual value into the array
'
sRetVal(iRow, 1) = drReader.Item(iRow).ToString
iRow += 1
Next
Return sRetVal
End Function
 
Back
Top