473,320 Members | 1,881 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,320 software developers and data experts.

Send refrence types by value to a method

A.M
Hi,

Ho can I send a refrence type by value as a method parameter.

Thanks,
Alan
Nov 17 '05 #1
6 1436
A.M <no******@online.nospam> wrote:
Ho can I send a refrence type by value as a method parameter.


Well, the value of a reference type expression is a reference, and that
is passed by value by default.

You can't pass an actual *object* either by value or by reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Just by passing the reference type as parameter to a method without using
the ref or out keyword. You create a copy of the reference pointer on the
stack that points to the object on the heap..

Gabriel Lozano-Morán
Software Engineer
Sogeti

"A.M" <no******@online.nospam> wrote in message
news:u1*************@TK2MSFTNGP12.phx.gbl...
Hi,

Ho can I send a refrence type by value as a method parameter.

Thanks,
Alan

Nov 17 '05 #3
To add to Jon's and Gabriel's comments, if you want to pass a copy of
the object, it must be an object that implements ICloneable, and you
would clone it:

myObject.MyMethod(argumentObject.Clone());

Nov 17 '05 #4
Take in mind that this cloning is just a shallow copy

Gabriel Lozano-Morán
Software Engineer
Sogeti

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
To add to Jon's and Gabriel's comments, if you want to pass a copy of
the object, it must be an object that implements ICloneable, and you
would clone it:

myObject.MyMethod(argumentObject.Clone());

Nov 17 '05 #5
Yes, usually. Of course, it depends upon how the class designer
implemented ICloneable, which never does specifically say whether a
clone should be a shallow copy or a deep copy.

Regardless, yes, a clone usually copies only the fields that are part
of the object being cloned, and does not copy objects to which the
cloned object holds references. So, in essence, by cloning you get:

1. A new object.
2. New copies of any value types (ints, doubles, DateTimes, structs)
that the object holds.
3. References to the same reference type objects to which the original
object held references.

Again, this depends upon how the class designer implemented ICloneable.

Of course, if the class doesn't implement ICloneable, then you can't
copy it using Clone(), and you only option is to create a new one using
"new" and whatever fields you can get at. Some classes (for example
System.Drawing.Font) work this way.

Nov 17 '05 #6
OK... so System.Drawing.Font implements ICloneable. D-Oh! Should have
checked first.

Anyway, there _are_ some classes that don't implement ICloneable, and
in those cases your only option is to use "new" and construct a new
object passing the properties of the object you want to copy.

Nov 17 '05 #7

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

Similar topics

1
by: malborg | last post by:
I have simple form in PHP with two input types as text to input login name and password.PS: They use POST method. (index.php) The other script(logowanie.php) checks if login and password is...
1
by: Rafael Veronezi | last post by:
Just to fix, correct me if I am wrong... With reference types (objects), if I assign an object to another, the assignment will be the address of the object, and not a copy of it's contents right?...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i don´t want to filter the return value of the method, i have to pass a new instance of the...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
4
by: Learner | last post by:
Hello, I have database field called 'PullAHead' defined as a bit field. Now if user doesn't pick a 'Yes' or 'No' in the front I need to be able to send a null value into the 'PullAHead' field in...
5
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.