473,326 Members | 2,337 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,326 software developers and data experts.

pass objects by value

HI,

I am passing an object to a function that will do some changes in the
object.. I need my object to be the same when the function has done it's
job.. How can I pass my object, do some changes in it and still keep the
original obect????
--
LZ
Jul 26 '06 #1
3 1502
Lamis wrote:
I am passing an object to a function that will do some changes in the
object.. I need my object to be the same when the function has done it's
job.. How can I pass my object, do some changes in it and still keep the
original obect????
You'll need to create a copy (or clone) of your object. Quite how you
do that depends on the type of object you're talking about. Bear in
mind that just copying the contents blindly may not make as deep a copy
as you expect - if your object contains a list, then creating a new
object by copying the list *reference* will still use the existing
list...

Jon

Jul 26 '06 #2
By "object", I'm assuming you mean a class, otherwise the question is
trivial.

So you mean e.g. that a "Person" class has a "Name", and "SomeFunc" accepts
a "Person" instance, changes the "Name". You want, when leaving "SomeFunc",
for the original "Name" to still be there? Well, you can't.

You need to *either* not monkey with the object inside "SomeFunc" (the ideal
situation), or you need to explicitely clone the object before calling the
method, and either pass the throw-away clone, or pass the original and then
revert afterwards (the first is easier!). Worst case (if you can't change
the object to make it cloneable) you will need to take a snapshot of the
properties that will get trashed, and restore them afterwards, ideally in a
"finally" block.

Why do you need this? If you can explain more about your intent, there may
be better options...

Marc
Jul 26 '06 #3
LZ,

You can't. Since it is a reference type, the only thing that the
function guarantees will be the same on return is the reference that you
pass (assuming that the parameter is not marked ref or out).

That being said, you have some options.

The first is to copy the object.

The second is to use a structure. However, this could change how your
app works, depending on how extensively it is used.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lamis" <La***@discussions.microsoft.comwrote in message
news:90**********************************@microsof t.com...
HI,

I am passing an object to a function that will do some changes in the
object.. I need my object to be the same when the function has done it's
job.. How can I pass my object, do some changes in it and still keep the
original obect????
--
LZ

Jul 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
9
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a...
2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.