passing object to method doesn't update object

B

Bruce Wood

TS said:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the object is a reference type?

my code is not proving that. I have a web project i created from a web
service that is my object:
public class ExcelService : SoapHttpClientProtocol

{

public ExcelService(); ...

In my code in initialize the object varialbe to null and then pass it to a
method that instantiates it, but when the method returns the varaible is
still null

ExcelService es = null;
string sessionId = OpenSession(es);

es equals null here!!!!!

Yes is does. Your is a classic misunderstanding of the difference
between "pass reference by value" and "pass by reference". Read Jon
Skeet's article on parameter passing and pay particular attention to
that bit:

http://www.yoda.arachsys.com/csharp/parameters.html
 
T

TS

I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the object is a reference type?

my code is not proving that. I have a web project i created from a web
service that is my object:
public class ExcelService : SoapHttpClientProtocol

{

public ExcelService(); ...

In my code in initialize the object varialbe to null and then pass it to a
method that instantiates it, but when the method returns the varaible is
still null

ExcelService es = null;
string sessionId = OpenSession(es);

es equals null here!!!!!

thanks
 
K

Kevin Yu [MSFT]

Hi TS,

Yes, I agree with Bruce, that you have to pass the parameter by reference
instead of pass reference by value. You can try the following when defining
your method.

OpenSession(ref ExcelService ExcelS)

When calling, you can use

OpenSession(ref es);

HTH.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

TS,
If it is a webservice, then you are not "passing" the actual object, but a
proxy xml serialized representation of the object. Expecting a webservice to
handle this as if it were a reference type is not feasible.
Peter
 
C

Cor Ligthert [MVP]

TS,

In my idea are you writing it yourself. (In slightly other words bellow)

You are passing a reference to "zero objects" to a method, and when you are
changing things in your method those "zero objects" will not be changes.

If you want to sent a to be used reference than you have to send that
reference and not the "zero objects".

Cor
 
K

KWienhold

More precisely speaking, a reference object is basically a pointer to
the memory location that holds the actual data. When you pass a
reference object by value to a function this pointer is copied to a new
pointer instance that will only be valid inside the function.
Normally this doesn't make much of a difference to passing it by
reference, since you will still be making changes to the same data in
memory, only through two different pointers (the original one if passed
by ref, the copy if passed by value).
When you pass a null reference by value, the pointer points to no data
yet, so when it is copied you receive a new null pointer. Initializing
this pointer won't change the fact that your original pointer still
points to nothing, that's why your object is still null after the
function returns.
If you initialize your object with a new instance first, pass that to
the function and alter its properties, you should see the changes you
are looking for, although the same can be achieved by simply passing it
as a reference.

Sincerely,
Kevin Wienhold
 
S

Sericinus hunter

You assumption is correct in a sense that you can use this
variable to access the object: call its methods, access members.
But you cannot assign whole another object to this variable.
Well, you can, but it won't be visible outside the function.
 
T

TS

OK, this is the one that did it for me. i understand the error of my
thinking. I was remembering what i read in the help system:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/csref/html/vclrfPassingMethodParameters.htm#vclrfpassingmethodparameters_referencetypes
Passing Reference-Type Parameters
A variable of a reference type does not contain its data directly; it
contains a reference to its data. When you pass a reference-type parameter
by value, it is possible to change the data pointed to by the reference,
such as the value of a class member. However, you cannot change the value of
the reference itself; that is, you cannot use the same reference to allocate
memory for a new class and have it persist outside the block. To do that,
pass the parameter using the ref (or out) keyword. For simplicity, the
following examples use ref.

Example 4: Passing Reference Types by Value
The following example demonstrates passing a reference-type parameter,
myArray, by value, to a method, Change. Because the parameter is a reference
to myArray, it is possible to change the values of the array elements.
However, the attempt to reassign the parameter to a different memory
location only works inside the method and does not affect the original
variable, myArray.
....

So my original assumption was correct, but as you pointed out, i
instantiated my object to null, so the reference i sent was null and you
cannot do anything with a null reference. If i first instantiate the object
and then pass it by value, changing the object in the method has an effect
on the object.

Thanks for clearing the fog on that one!
TS
 

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