473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

passing object to method doesn't update object

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
7 3271

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
3
by: Marc Castrechini | last post by:
First off this is a great reference for passing data between the Data Access and Business Layers:...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
7
by: J055 | last post by:
Hi I'm a little confused by this so would appreciate some advice on ways to deal with the following. When I call the CodeMaker.Update static method for the second time I want the dt parameter to...
4
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
0
by: Magnus Bergh | last post by:
I am developing an application for pocketpc and this involvs a but of juggling with different forms. I have an "order entry" type of application. On the main form I have a grid which displays...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.