to be an object or not to be an object

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
97
Programming Experience
10+
I am in the process of learning this new programming paradigm called OOP (I am a very seasoned structured programmer). In that context I am receiving what I call contradictions in vb.net regarding the instantiation of an object. Given the two lines of code below:

Dim SQLcmd As New SqlCommand
Dim SQLdr As SqlDataReader

The first line is quite simple - SQLcmd is an object - period. It is the second line that is creating the ambiguity. One reference I have read says in order for a class to instantiate an object the key word "new" must be used. But I have also seen examples where SQLdr is also called an object? To add to the ambiguity I have seen class examples where the class was constructed without a new subroutine (a constructor) and called with the key word "new" which I have been informed by vb.net on more than one occasion in doing such that "there are no constructors" in the class. So would someone please tell me - Is SQLdr an object and if so is it by inheritance (the other escape path) or what; and lastly am I correct in my understanding that in order to use the key word "new" there must be a "new" subroutine/function/constructor in the class. I thank you for your time and expertise.

Respectfully,
Gordon Haas
 
This is all you need: Value Types and Reference Types

About your code; first line is short for
Dim SQLcmd As SqlCommand = New SqlCommand

which means (A) declare a variable named 'SQLcmd' of type SqlCommand, (B) create an object of type SqlCommand (using parameterless constructor), and (C) assign the object to the variable.

Second line simply means declare a variable named 'SQLdr' of type SqlDataReader.
 
Thank you very much JohnH. I will visit the link and digest its information. So from what you are telling me SQLdr is not a object. And the definition that - for a class to instantiate an object the key word "new" must be used in the declaration and there must exist a constructor in the class - is valid? I will most probably find the answer to my next perplexion at the web site you referenced but I will present it to you in hopes of saving time. In the code statement below that FOLLOWS the initial 2 code statements I inquired about:

SQLdr = SQLcmd.ExecuteReader

is SQLdr now an object? And was this creation the result of transference by way of the object SQLcmd and was SQLcmd's method/function ExecuteReader placed in SQLdr's new "object world" or is it a matter of simple pointer (indirection) mechanics to permit utility? Please excuse me if I am asking "unnecessary" questions but in the world I am from (mathematics) even implicit variables still need to be defined/understood!

Respectfully,
Gordon Haas
 
So from what you are telling me SQLdr is not a object.
Correct, it is a variable.
to instantiate an object the key word "new" must be used in the declaration and there must exist a constructor in the class - is valid?
New is a keyword used to invoke a type constructor, and since a Structure also can have constructors the New keyword can also be used to create a value.
SQLdr = SQLcmd.ExecuteReader
SqlCommand.ExecuteReader is a function whose return type is SqlDataReader. SqlDataReader is a class, so what is returned is an object reference and not a value.
= is here the assignment operator.
The result of the expression is that the variable now holds a reference to the object that was returned.
The object is somewhere in memory and multiple variables can reference it.
is SQLdr now an object?
No, it is still a variable.
 
Back
Top