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

Question about passing reference type to a method

When I pass a reference to a method by value, the reference argument to the
method is copied but still points to the original object, and that method
can modify the object. Can I apply const keyword to the argument to prevent
this?

f.e.,

instead of:

void Method(mytype obj)
{
// can modify obj
}

do:

void Method(const mytype obj)
{
// can't modify obj
}
Nov 15 '05 #1
3 1003
No, you can't. What's your use case here? Why are you
wanting to do this?

Richard
-----Original Message-----
When I pass a reference to a method by value, the reference argument to themethod is copied but still points to the original object, and that methodcan modify the object. Can I apply const keyword to the argument to preventthis?

f.e.,

instead of:

void Method(mytype obj)
{
// can modify obj
}

do:

void Method(const mytype obj)
{
// can't modify obj
}
.

Nov 15 '05 #2
Hi Alex,

No, you can't this have been subject of debate before, if you don't want
your object to be change and you know for sure or donot know if the calling
method will modify it you will need to clone it before.

Take a look at this thread:
http://groups.google.com/groups?hl=e...%26scoring%3Dd

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alex" <a.*******@verizon.net> wrote in message
news:O0**************@TK2MSFTNGP11.phx.gbl...
When I pass a reference to a method by value, the reference argument to the method is copied but still points to the original object, and that method
can modify the object. Can I apply const keyword to the argument to prevent this?

f.e.,

instead of:

void Method(mytype obj)
{
// can modify obj
}

do:

void Method(const mytype obj)
{
// can't modify obj
}

Nov 15 '05 #3
On [GMT+0100=CET],
Ignacio Machin <ignacio.machin AT dot.state.fl.us> thought hard and spewed:
Hi Alex,

No, you can't this have been subject of debate before, if you don't
want your object to be change and you know for sure or donot know if
the calling method will modify it you will need to clone it before.


Thank you, I hope that the C# designers take heed and add const parameters!
Nov 15 '05 #4

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

Similar topics

26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays 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,...
6
by: Chris | last post by:
Hi all, I've recently discovered the joys of tag :) I have a ListView which is populated with strings from a List<>, and each ListView item is linked to the relevant object in List<> using Tag....
7
by: TS | last post by:
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...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.