By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

passing object to method doesn't update object

P: n/a
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
Nov 9 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

TS wrote:
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

Nov 9 '06 #2

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Nov 10 '06 #3

P: n/a
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"TS" wrote:
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
Nov 10 '06 #4

P: n/a
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

"TS" <ma**********@nospam.nospamschreef in bericht
news:uM**************@TK2MSFTNGP02.phx.gbl...
>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


Nov 10 '06 #5

P: n/a
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

Cor Ligthert [MVP] schrieb:
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

"TS" <ma**********@nospam.nospamschreef in bericht
news:uM**************@TK2MSFTNGP02.phx.gbl...
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
Nov 10 '06 #6

P: n/a
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.

TS wrote:
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

Nov 10 '06 #7

P: n/a
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#vclrfpassingmetho dparameters_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
"KWienhold" <he******@trashmail.netwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
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

Cor Ligthert [MVP] schrieb:
>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

"TS" <ma**********@nospam.nospamschreef in bericht
news:uM**************@TK2MSFTNGP02.phx.gbl...
>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


Nov 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.