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 GetEmployeeCert ifications,
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 GetEmployeeCert ifications function,
has appended to it the parameter objects from the FillParameters
function. How can that be?
Public Shared Function GetEmployeeCert ifications(ByVa l criteria As
SearchCriteria) As EmployeeCertifi cationCollectio n
Dim tblEmployees As New DataTable
Dim connection As New SqlConnection(c onnectionString )
Dim cmdGetEmployees As New SqlCommand
Dim myDataAdapter As New SqlDataAdapter
Dim employees As New EmployeeCertifi cationCollectio n
Try
connection.Open ()
cmdGetEmployees .Connection = connection
cmdGetEmployees .CommandText = "GetEmployeeCer tifications"
cmdGetEmployees .CommandType = CommandType.Sto redProcedure
FillParameters( criteria, cmdGetEmployees ) '<---Here is the
call with no assignment
myDataAdapter.S electCommand = cmdGetEmployees
myDataAdapter.F ill(tblEmployee s)
Catch ex As Exception
Finally
'convert the matching data rows to an
EmployeeCertifi cationCollectio n
If Not tblEmployees Is Nothing Then
Dim row As DataRow
For Each row In tblEmployees.Ro ws
employees.Add(G etEmployeeCerti fication(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.CostCe nterFrom.Length 0 Or
criteria.CostCe nterTo.Length 0 Then
employees.Apply Sort("CostCente r",
ComponentModel. ListSortDirecti on.Ascending)
End If
Return employees
End Function
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
If criteria.LastNa me.Length 0 Then
cmd.Parameters. Add(New SqlParameter("@ LastName",
SqlEscape(crite ria.LastName)))
End If
If Not (criteria.SiteC ode = "ALL" Or criteria.SiteCo de.Length =
0) Then
cmd.Parameters. Add(New SqlParameter("@ SiteCode",
criteria.SiteCo de))
End If
End Function 52 3193
Julie <ju**********@y ahoo.comwrote:
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 GetEmployeeCert ifications,
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
You're confused about parameter passing, basically. See http://www.pobox.com/~skeet/csharp/parameters.html
It's written in terms of C#, but the principle is the same - even when
a parameter is passed by value, if it's a reference type (such as
SqlCommand) it's the *reference* which is passed. Changes to the object
made via that reference are still visible to the caller after the
method has terminated.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
FillParameters( criteria, cmdGetEmployees ) '<---Here is the
call with no assignment
This is a call by reference. A Command object is by reference always.
So the parameters are filled using the reference.
Jesse
Jesse Houwing <je***********@ nospam-sogeti.nlwrote:
FillParameters( criteria, cmdGetEmployees ) '<---Here is the
call with no assignment
This is a call by reference. A Command object is by reference always.
No, the object isn't passed at all. There's a different between passing
an object by reference and passing a reference by value. See http://www.pobox.com/~skeet/csharp/parameters.html for more on this.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon,
Thanks for the refresher on reference types vs value types. The
confusion comes in on the variable definition in the function name with
the explicit ByVal keyword.
A variable can be assigned a reference. A second variable can be
assigned the value of first. "They are still, however, independent
variables themselves. Changing the value of first will not change the
value of second". But change the value of the object using the first
variable will change the second.
StringBuilder first = new StringBuilder() ;
StringBuilder second = first;
first.Append ("hello");
first = null;
Console.WriteLi ne (second);
Hello will still print because second is a variable whose address
points to an area in core that contains hello. While first has
released its address.
HERE IS MY DISCONNECT
The variable cmd is defined as a SQL command type object. But the
ByValue keyword threw me as we are passing the pointer not creating a
duplicate object. It is possible to create and object in memory that
has the same contents as another object in memory. Thus cmd and
cmdGetEmployees are two different variables pointing to the same
object.
So in the FillParameters function what would have happened if I had
ByVal with a new keyword?
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As new SqlCommand)
Thanks for the whack upside the head.
J
Jon wrote:
Jesse Houwing <je***********@ nospam-sogeti.nlwrote:
FillParameters( criteria, cmdGetEmployees ) '<---Here is the
call with no assignment
This is a call by reference. A Command object is by reference always.
No, the object isn't passed at all. There's a different between passing
an object by reference and passing a reference by value. See http://www.pobox.com/~skeet/csharp/parameters.html for more on this.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Julie <ju**********@y ahoo.comwrote:
<snip>
So in the FillParameters function what would have happened if I had
ByVal with a new keyword?
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As new SqlCommand)
I'm afraid I don't know enough VB.NET to understand that syntax - it
doesn't seem to make sense judging by the documentation.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon,
No, the object isn't passed at all. There's a different between passing
an object by reference and passing a reference by value. See
That was in my idea not written by Jesse. I think that we are blind as soon
as we read "reference" .
A reference type is always passing in any way an address as the reference
not the complete object.
:-)
Cor
Julie,
Do you know why this methode declaration bellow does not give an error. You
write this as a Sub, which should work, but better is it to avoid this in
this kind of situations where you are changing values inside a Sub.
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
Try to avoid this, try to return what has to be returned, that makes your
program at least more readable.
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand) as SqlCommand
....
return cmd
And than in your code
cmdGetEmloyees = FillParameters( criteria, cmdGetEmployees )
However the strangest in your code is for me that function whithout return
value.
I hope this helps,
"Julie" <ju**********@y ahoo.comschreef in bericht
news:11******** *************@m 73g2000cwd.goog legroups.com...
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 GetEmployeeCert ifications,
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 GetEmployeeCert ifications function,
has appended to it the parameter objects from the FillParameters
function. How can that be?
Public Shared Function GetEmployeeCert ifications(ByVa l criteria As
SearchCriteria) As EmployeeCertifi cationCollectio n
Dim tblEmployees As New DataTable
Dim connection As New SqlConnection(c onnectionString )
Dim cmdGetEmployees As New SqlCommand
Dim myDataAdapter As New SqlDataAdapter
Dim employees As New EmployeeCertifi cationCollectio n
Try
connection.Open ()
cmdGetEmployees .Connection = connection
cmdGetEmployees .CommandText = "GetEmployeeCer tifications"
cmdGetEmployees .CommandType = CommandType.Sto redProcedure
FillParameters( criteria, cmdGetEmployees ) '<---Here is the
call with no assignment
myDataAdapter.S electCommand = cmdGetEmployees
myDataAdapter.F ill(tblEmployee s)
Catch ex As Exception
Finally
'convert the matching data rows to an
EmployeeCertifi cationCollectio n
If Not tblEmployees Is Nothing Then
Dim row As DataRow
For Each row In tblEmployees.Ro ws
employees.Add(G etEmployeeCerti fication(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.CostCe nterFrom.Length 0 Or
criteria.CostCe nterTo.Length 0 Then
employees.Apply Sort("CostCente r",
ComponentModel. ListSortDirecti on.Ascending)
End If
Return employees
End Function
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
If criteria.LastNa me.Length 0 Then
cmd.Parameters. Add(New SqlParameter("@ LastName",
SqlEscape(crite ria.LastName)))
End If
If Not (criteria.SiteC ode = "ALL" Or criteria.SiteCo de.Length =
0) Then
cmd.Parameters. Add(New SqlParameter("@ SiteCode",
criteria.SiteCo de))
End If
End Function
Cor Ligthert [MVP] <no************ @planet.nlwrote :
No, the object isn't passed at all. There's a different between passing
an object by reference and passing a reference by value. See
That was in my idea not written by Jesse. I think that we are blind as soon
as we read "reference" .
A reference type is always passing in any way an address as the reference
not the complete object.
Yes, but Jesse claimed that the object was passed by reference. It
isn't. It isn't passed at all. There are well-defined meaning to "pass
by reference", and ByVal semantics aren't the same, even when what is
passed is a reference.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Cor Ligthert [MVP] <no************ @planet.nlwrote :
Do you know why this methode declaration bellow does not give an error. You
write this as a Sub, which should work, but better is it to avoid this in
this kind of situations where you are changing values inside a Sub.
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
Try to avoid this, try to return what has to be returned, that makes your
program at least more readable.
Private Shared Function FillParameters( ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand) as SqlCommand
...
return cmd
And than in your code
cmdGetEmloyees = FillParameters( criteria, cmdGetEmployees )
However the strangest in your code is for me that function whithout return
value.
Why is that strange? To me, it would be very odd to return the command
when there's no need for it. Why have a return value when it's
unnecessary? *That* would make the code less readable IMO.
Rather than pretending that the return value is important, it's better
to understand reference type semantics. What would you do when the
method modified the objects referred to by two of its parameters? You
can't return them both, so you get back to the same problem of
understanding.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jonas |
last post by:
This works fine in Win XP but does not work at all in Win 98.
Private WithEvents objIExplorer As InternetExplorer
I have to do it like this to get it to work in Win 98
Dim objIExplorer As InternetExplorer But then I cant see when a user exit my
objIExplorer and than an error will show up when I try to open a link in the
IE window that does not exist...
What can I do about this and way does it not work in win 98?
|
by: Paul THompson |
last post by:
I have a piece of code something like the following:
START OF CODE
<html><head><title>The Wizard</title></head>
<body> <h1>Welcome to Joe's Vet Clinic</h1>
<div id="part1" style="position:absolute;top:150px;left:50px;visibility:visible;">
<form name="visitor_info">
<table><tr><td>Your name:</td><td><input name="visname"
type="text"></td></tr>
<tr><td>Your phone number:</td>
|
by: Isaac Grover |
last post by:
Hi everyone,
Just out of curiosity I recently pointed one of my hand-typed pages at the W3
Validator, and my hand-typed code was just ripped to shreds. Then I pointed
some major sites (microsoft.com, cnn.com, etc.) at the W3 Validator; to my
surprise none of them passed.
Doesn't anyone care anymore, or are the standards more-or-less looked at as
guidlines for web design?
|
by: me |
last post by:
I have a Class Library that contains a Form and several helper classes. A
thread gets created that performs processing of data behind the scenes and
the Form never gets displayed (it is for debug puposes only and is not
normally visable to the user.) The Thread function is actually in the Form
class.
Now.. What I am seeing is that when I create an instance of this Class
Library's Form, which starts the worker thread, it seems to hose up...
|
by: rick |
last post by:
The following basic script works fine in firefox by not in IE. Can anyone
spot the problem? In IE I can only delete the first line but not the lines
created by javascript. Also, look at the HTML code for the first line
(click the Table HTML button:)) you will fine that the code displayed is not
the same as
was written. "onChange" was changed to "onchange" etc. Please help.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">...
| |
by: benb |
last post by:
I have form that looks a lot like a search bar for the user to search
for records matching specified criteria (e.g. first names containing
"ben"). For robust results, an intermediary form displays all records
matching the criteria (hmm...sound like a popular web site I know).
Here is the immediate problem: my code (below) works fine on my
computer, but on some of those belonging to my work colleagues (who
will be the end users of the db)...
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it can be done using the designer
but I intentionally don't want to use that. The one reason is that you
cannot change the code generated by the designer. The other could be that
you have more free hand and control to design your GUI.
2....
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be used."
Could anybody tell me why gets() function is dangerous??
Thank you very much.
Cuthbert
|
by: webEater |
last post by:
I have a problem, it's not browser specific, and I don't get a
solution. I have an (X)HTML document, I show you a part of it:
....
<!--<div class="pad">-->
<div id="eventImages"><img src="" id="eventImage0"
class="eventImage"><img src="" id="eventImage1"
class="eventImage"></div>
|
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= |
last post by:
I get the above error in some of the ASP.NET web applications on a
server, and I need some help figuring out how to deal with it.
This is a rather long post, and I hope I have enough details that
someone who bothers to read all of it have some pointers.
Note, I have posted the stack trace and the code exhibiting the problem
further down so if you want to start by reading that, search for +++
Also note that I am unable to reproduce...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |