473,756 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 : SoapHttpClientP rotocol

{

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 3305

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 : SoapHttpClientP rotocol

{

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 misunderstandin g 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 : SoapHttpClientP rotocol

{

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**********@n ospam.nospamsch reef in bericht
news:uM******** ******@TK2MSFTN GP02.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 : SoapHttpClientP rotocol

{

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**********@n ospam.nospamsch reef in bericht
news:uM******** ******@TK2MSFTN GP02.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 : SoapHttpClientP rotocol

{

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 : SoapHttpClientP rotocol

{

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.2003 FEB.1033/csref/html/vclrfPassingMet hodParameters.h tm#vclrfpassing methodparameter s_referencetype s
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******@trash mail.netwrote in message
news:11******** *************@k 70g2000cwa.goog legroups.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**********@n ospam.nospamsch reef in bericht
news:uM******* *******@TK2MSFT NGP02.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 : SoapHttpClientP rotocol

{

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
6557
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 courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html I am passing countries, products, and courses. The first two display
58
10174
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 code... TCHAR myArray; DoStuff(myArray);
8
2118
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, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
3
2684
by: Marc Castrechini | last post by:
First off this is a great reference for passing data between the Data Access and Business Layers: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/Anch_EntDevAppArchPatPrac.asp I use my own classes in the Business layer. I want to keep the Data Access layer from requiring these classes so I tried passing a Datarow between the layers and it seems to work good for me. Constructing the datarow in the Class...
22
25600
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 compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet = ws.VerifySku(cartSet)
7
1622
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 have the same value as when called the first time. What would be the best approach (and simplest) way to deal with this? Cheers Andrew
4
4012
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 is whats the best way to pass large numbers of parameters into a method. Ten seems a large number to be passing into and out of a method. Stephen
9
3800
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
0
1544
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 Order headers. Let call this form "OrderList" From this view I edit/enter new orders by opening a new form for entering data. This is done using (more or less) the designer generated forms, so I have a "Order edit view dialog. I pass the binding...
0
9152
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9930
Oralloy
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...
0
9716
jinu1996
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...
0
9571
tracyyun
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...
1
7116
isladogs
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...
0
6410
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();...
1
3676
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
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
bsmnconsultancy
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...

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.