New operator (trying to understand)

M

Morten Snedker

As I understand the New operator is used to create a new instance of
an object, e.g:

Imports ss = System.data.sqlclient
....

Dim sCon As String, sql As String, dbCon As ss.SqlConnection
Dim dbCmd As ss.SqlCommand, dbReader As ss.SqlDataReader

sCon = "Server=blah;Database=bluh;Integrated Security=SSPI;"
sql = "SELECT bbr FROM SagsAktivitet ORDER BY bbr"

Try
dbCon = New ss.SqlConnection(sCon)
dbCon.Open()
dbCmd = New ss.SqlCommand(sql, dbCon)
dbReader = dbCmd.ExecuteReader
....


New is used on SqlConnection and on SqlCommand.

Why isn't it used on dbReader? Isn't dbReader an object that is
created?

Thanx for any enlightment! :)


/Snedker
 
C

Chris

Morten said:
As I understand the New operator is used to create a new instance of
an object, e.g:

Imports ss = System.data.sqlclient
...

Dim sCon As String, sql As String, dbCon As ss.SqlConnection
Dim dbCmd As ss.SqlCommand, dbReader As ss.SqlDataReader

sCon = "Server=blah;Database=bluh;Integrated Security=SSPI;"
sql = "SELECT bbr FROM SagsAktivitet ORDER BY bbr"

Try
dbCon = New ss.SqlConnection(sCon)
dbCon.Open()
dbCmd = New ss.SqlCommand(sql, dbCon)
dbReader = dbCmd.ExecuteReader
...


New is used on SqlConnection and on SqlCommand.

Why isn't it used on dbReader? Isn't dbReader an object that is
created?

Thanx for any enlightment! :)


/Snedker

The line dbCmd.ExecuteReader returns an instance of SqlDataReader for
you. So somewhere inside ExecuteReader they do the "new" for you
basically.

Chris
 
A

_AnonCoward

"Morten Snedker" <morten_spammenot_ATdbconsult.dk> wrote in message
:
: As I understand the New operator is used to create a new instance of
: an object, e.g:
:
: Imports ss = System.data.sqlclient
: ...
:
: Dim sCon As String, sql As String, dbCon As ss.SqlConnection
: Dim dbCmd As ss.SqlCommand, dbReader As ss.SqlDataReader
:
: sCon = "Server=blah;Database=bluh;Integrated Security=SSPI;"
: sql = "SELECT bbr FROM SagsAktivitet ORDER BY bbr"
:
: Try
: dbCon = New ss.SqlConnection(sCon)
: dbCon.Open()
: dbCmd = New ss.SqlCommand(sql, dbCon)
: dbReader = dbCmd.ExecuteReader
: ...
:
:
: New is used on SqlConnection and on SqlCommand.
:
: Why isn't it used on dbReader? Isn't dbReader an object that is
: created?
:
: Thanx for any enlightment! :)
:
:
: /Snedker


The new instance of the SQL data reader is created by the SQL command
object and the reference of that newly created object is assigned to the
dbReader variable.


Ralf
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top