Re: Why does this code work?
- From: "Ciaran" <ciaran@xxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 10 Jul 2006 23:31:11 +0100
This works because the command is a reference type. That means the VALUE
that is passed if the address of the object. That means that there are now
to references to the same object. One in the function and one original one
outside the function. Changing property values on the object, like adding
parameters, will affect both.
If in the function however you wrote:
cmd = new Command()
then the reference would be replace with one to a new object and they would
no longer point at the same object.
If the parameter was passed by reference, then the value passed to the
function is a reference to the reference to the object. This has the same
affect with the properties but also means using the new keyword as above
would update the reference in the calling function too.
Ciaran
There are 10 types of people in this world, those that understand binary,
and those that don't.
"Julie" <julie6232000@xxxxxxxxx> wrote in message
news:1151964732.467030.82930@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm supporting an application at work. Below are some code segments
that I can't understand how they work. First let me say, I would never
code this standard. I'm just really creeped out that it works.
Here's the setup. I have a function, called GetEmployeeCertifications,
that is going to make a call to a SQL DB and return a result set. This
module calls another function, called FillParameters, to determine if
SQL parameters need to be appended to the command object. The
FillParameters function...
does not have a return value
the command object is not globally defined
and the command object is passed by value not reference
But yet the command object, in the GetEmployeeCertifications function,
has appended to it the parameter objects from the FillParameters
function. How can that be?
Public Shared Function GetEmployeeCertifications(ByVal criteria As
SearchCriteria) As EmployeeCertificationCollection
Dim tblEmployees As New DataTable
Dim connection As New SqlConnection(connectionString)
Dim cmdGetEmployees As New SqlCommand
Dim myDataAdapter As New SqlDataAdapter
Dim employees As New EmployeeCertificationCollection
Try
connection.Open()
cmdGetEmployees.Connection = connection
cmdGetEmployees.CommandText = "GetEmployeeCertifications"
cmdGetEmployees.CommandType = CommandType.StoredProcedure
FillParameters(criteria, cmdGetEmployees) '<---Here is the
call with no assignment
myDataAdapter.SelectCommand = cmdGetEmployees
myDataAdapter.Fill(tblEmployees)
Catch ex As Exception
Finally
'convert the matching data rows to an
EmployeeCertificationCollection
If Not tblEmployees Is Nothing Then
Dim row As DataRow
For Each row In tblEmployees.Rows
employees.Add(GetEmployeeCertification(row))
Next
End If
End Try
'If they searched by cost center, then return the results
sorted by cost center.
'The results come back from the database ordered by last name.
If criteria.CostCenterFrom.Length > 0 Or
criteria.CostCenterTo.Length > 0 Then
employees.ApplySort("CostCenter",
ComponentModel.ListSortDirection.Ascending)
End If
Return employees
End Function
Private Shared Function FillParameters(ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
If criteria.LastName.Length > 0 Then
cmd.Parameters.Add(New SqlParameter("@LastName",
SqlEscape(criteria.LastName)))
End If
If Not (criteria.SiteCode = "ALL" Or criteria.SiteCode.Length =
0) Then
cmd.Parameters.Add(New SqlParameter("@SiteCode",
criteria.SiteCode))
End If
End Function
.
- References:
- Why does this code work?
- From: Julie
- Why does this code work?
- Prev by Date: Re: Auto dialer components?
- Next by Date: Re: Automate a message box based on UPDATE?
- Previous by thread: Re: Why does this code work?
- Next by thread: Re: Copying a project in VS .Net 2003 Pro
- Index(es):
Relevant Pages
|