Re: Why does this code work?



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



.



Relevant Pages

  • Re: Command line devenv not picking up dependencies on managed p
    ... commandline and it does not copy any of the C# reference assemblies to ... the build log shows that the xcopy command is incorrect. ... >> already been copied to the output folder by the manual compile. ...
    (microsoft.public.vsnet.ide)
  • Re: Where Is a Functional Characteristics for 3592 Tape Subsystem
    ... 3490 drives. ... I'd suggest SCSI command reference. ... For IBM-MAIN subscribe / signoff / archive access instructions, ...
    (bit.listserv.ibm-main)
  • Re: In text authors list with numerical referencing style
    ... command, and it made its debut in "named.sty" back in 1992. ... I was suggesting the \citeauthor command from natbib. ... I've done it via custombib -- as I recall, you simply create an author-date style that is compatible with natbib, but instruct natbib in your preamble to use a numerical citation format. ... As I recall, biblatex has a \citeauthor command analogous to that of natbib (i.e., it will give you the authors' names without printing a reference label). ...
    (comp.text.tex)
  • Re: Need help with intercepting the "Add reference" command
    ... > I am starting the IDE, clicked the menu item for registering, open project ... > and click on Add reference the BeforeCommandExecute is NOT invoked. ... Below you have the code to register command events ...
    (microsoft.public.vsnet.ide)
  • Opening Outlook from VB - second post
    ... I turned on the reference for Outlook and entered the ... I am using the SHELL command to open Outlook from VB ... > Dim Olook As Outlook.Application ...
    (microsoft.public.access.formscoding)