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 3 1965
1. When the function creates its DataReader object, it allocates a block of
memory for the object and puts a pointer to that memory into the variable
(drReader). When the function returns this object to the calling procedure,
it is actually just returning the pointer to the object. No second object is
created, just a second pointer to the original object.
Within the function itself, the "drReader" variable will go out of scope,
and will (eventually) be destroyed by the garbage collector. But it's just
the pointer that is being destroyed, not the object itself. Because there is
another reference to the object (the one received by the calling procedure
once your function completes), the object itself will not be destroyed. Once
the other reference to the object is set to Nothing or goes out of scope, it
is then possible for the object itself to be terminated and garbage
collected.
2. In this case, the DataReader must have been instantiated by the calling
function, otherwise it wouldn't have anything to pass into the drReader
parameter. Within the function, this parameter again is just a reference to
the existing object. It will use that object, and when the function returns
the reference will go out-of-scope (and later be garbage collected). As
before, the object itself is probably still referenced by the calling
procedure, so it will not be destroyed until that reference is released.
You do not need to destroy the object within the function, but you could set
the reference to Nothing if you wanted. This would not destroy the object
due to the calling procedure still having a reference to it, it would just
clear your local reference.
You shouldn't try to destroy or terminate the object by calling methods such
as Close(), either. You could do this (and in some circumstances it would be
correct to), but to do so would probably cause problems as the code calling
into your procedure would most likely not expect this to happen, and would
fail when it continued trying to use the object.
The thing to remember with object references is that they are just
references -- they're just a pointer to the object in memory, not the
physical object itself. When you pass or return a reference, that's all
you're passing, the pointer to the object. If you want to experiment with
this, take a look at the "Is" keyword. This will compare two object
references and tell you whether they point to the same object.
--
(O)enone
Miquel,
An object exist as long as there is a reference too that object. See my
comments beneath your objects. 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
You are returning here a new created drReader, so it will normally exist
(when you have not set the resulting reader global) until the end of the
method that calls this method exist (it exist something longer until it is
destroyed by the garbage collector, however you cannot use it anymore).
Because it is a shared Function everything will however exist for the
complete time of the program.
The way you do it is probably bad practise because you leave as I assume
your connection open all the time. See for that the text in your second
sample. 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
The datareader is not opened inside this function, it seems to be a global
datareader, so you should not close it as well in this function. Open and
close in the same method.
I personally would create the connection and the datareader and close the
datareader and dispose than the connection in this method. Than it will be
destroyed by the garbage collector because there is no reference anymore to
this.
I hope this gives some ideas
Cor
On Wed, 17 Nov 2004 11:07:46 +0200, Miguel wrote: In the example below I see 2 DataReader objects, one is called 'ExecuteReader' the other 'drReader, is drReader a pointer that then gets
There is only one DataReader, drReader. ExecuteReader is not a DataReader
but a function call. It calls an overloaded version of the ExecuteReader
function.
--
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: JustSomeGuy |
last post by:
Silly question maybe...
I pass a refrence to an object to a method...
The method adds the object to a (stl) list of these objects...
However if a refrence to the object is passed and then added...
|
by: justanotherguy63 |
last post by:
Hi,
I am designing an application where to preserve the hierachy and for
code substitability, I need to pass an array of derived class object in
place of an array of base class object. Since I...
|
by: Peter Ammon |
last post by:
Often times, I'll have some malloc()'d data in a struct that need not
change throughout the lifetime of the instance of the struct.
Therefore, the field within the struct is declared a pointer to...
|
by: Scott Zabolotzky |
last post by:
I'm trying to pass a custom object back and forth between forms.
This custom object is pulled into the app using an external reference
to an assembly DLL that was given to me by a co-worker. A...
|
by: jaymtz78 |
last post by:
Hi,
I have a huge windows application that I'm working on and I'm
completely baffled. Sometimes when I try to close the application, it
won't let me! I have an Exit button in my menu bar that...
|
by: Olumide |
last post by:
Hello -
I have two classes A and B as follows:
class B{
public:
~B(){ cout << "destroying B" << endl; }
};
class A{
|
by: Deckarep |
last post by:
Hello fellow C# programmers,
This question is more about general practice and convention so here
goes:
I got into a discussion with a co-worker who insisted that as a general
practice all...
|
by: Markus Pitha |
last post by:
Hello,
I have a class "Menu". In this class I declare an object "Controller".
Now I have a problem:
Controller uses a ctor but I get the value I have to pass later in my
program.
The only way...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| | |