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

Const Object Reference in C# function

I want to pass an Object Reference to a C# function, but also I want to make
sure that the calling function should not change it's value.
The C++ Code Like

MyClass cls;
Fun1(cls)

void Fun1(const MyClass & cls)
{
int a = cls.m_No;
}
I want to write same code in C#. How should I write because
you can not send Cont Object Reference to a function in C#.

--
Shreyas S. Ranade
Nov 15 '05 #1
3 5855
Shreyas, there isn't a straighforward way to do this. One work around would
be only providing a getter like Morten suggested. You could also use a
Field instead of a property and apply the readonly attribute to the field.
Not sure which will work with your exact scenario.

--
Greg Ewing [MVP]
http://www.claritycon.com/

"Shreyas Ranade" <sh************@hotmail.com> wrote in message
news:Ot*************@TK2MSFTNGP09.phx.gbl...
I want to pass an Object Reference to a C# function, but also I want to make sure that the calling function should not change it's value.
The C++ Code Like

MyClass cls;
Fun1(cls)

void Fun1(const MyClass & cls)
{
int a = cls.m_No;
}
I want to write same code in C#. How should I write because
you can not send Cont Object Reference to a function in C#.

--
Shreyas S. Ranade

Nov 15 '05 #2
My favorite way is to have an interface for every class which is
changeable -

ie
interface ConstMyClass
{
int number {get;}
string value {get;}
ConstMyClass CombineWith( ConstMyClass);
...
}

then have your class implement that
ie
class MyClass : ConstMyClass
{
....
}

then most of the places you use it, you can define
void Fun1( ConstMyClass x )
{
}

and be 100% guaranteed that your object isn't going to be manipulated.



"Shreyas Ranade" <sh************@hotmail.com> wrote in message
news:Ot*************@TK2MSFTNGP09.phx.gbl...
I want to pass an Object Reference to a C# function, but also I want to make sure that the calling function should not change it's value.
The C++ Code Like

MyClass cls;
Fun1(cls)

void Fun1(const MyClass & cls)
{
int a = cls.m_No;
}
I want to write same code in C#. How should I write because
you can not send Cont Object Reference to a function in C#.

--
Shreyas S. Ranade

Nov 15 '05 #3
Darren Oakey <yo*@darrenoakey.info> wrote:
My favorite way is to have an interface for every class which is
changeable -

ie
interface ConstMyClass
{
int number {get;}
string value {get;}
ConstMyClass CombineWith( ConstMyClass);
...
}

then have your class implement that
ie
class MyClass : ConstMyClass
{
...
}

then most of the places you use it, you can define
void Fun1( ConstMyClass x )
{
}

and be 100% guaranteed that your object isn't going to be manipulated.


Unfortunately it doesn't, actually - because the client can always
check whether or not the instance is *actually* an instance of MyClass,
cast it, and then manipulate it.

What you *can* do is have a wrapper which *contains* an instance of
MyClass, and delegates read access to it, eg:

class ConstMyClass
{
MyClass target;

public ConstMyClass (MyClass target)
{
this.target = target;
}

public int Number
{
get { return target.Number; }
}
}

etc

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
3
by: Giff | last post by:
Hi, I am trying to change my way of programming (I am still learning) , in particular I am putting an effort in passing const ref to functions, when possible. When possible means (to me) when...
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
3
by: George2 | last post by:
Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object? 2. If in (1), it is correct...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.