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

constant variable, local and passing to methods

How can I pass a reference to a method as constant?

I tried the following:

Function(const Foo f) or Function(readonly Foo f)
Also, How to declare local variable to be constant

const Foo foo or readonlyFoo f?
Oct 1 '08 #1
4 2102
On Wed, 01 Oct 2008 10:51:00 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
How can I pass a reference to a method as constant?

I tried the following:

Function(const Foo f) or Function(readonly Foo f)
You can't.
Also, How to declare local variable to be constant

const Foo foo or readonlyFoo f?
Well, when you tried it, which one worked and which one didn't? :)

Pete
Oct 1 '08 #2
On Oct 1, 2:18*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 01 Oct 2008 10:51:00 -0700, puzzlecracker <ironsel2...@gmail.com>*
wrote:
How can I pass a reference to a method as constant?
I tried the following:
Function(const Foo *f) or *Function(readonly Foo *f)

You can't.
Also, How to declare local variable to be constant
const Foo foo or readonlyFoo f?

Well, when you tried it, which one worked and which one didn't? *:)

Pete
I'd like to know the standard way to send reference type as const? I
suppose no need for that for value types since they copied anyway.

What if we send value types, not as const, and it contains a
references type as a member. Then reference member will be modified,
hence there is a need to send even value types as const if such
behavior is desired. Now, please tell me how it's done.

Thanks
Oct 1 '08 #3
On Wed, 01 Oct 2008 11:36:06 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
I'd like to know the standard way to send reference type as const? I
suppose no need for that for value types since they copied anyway.

What if we send value types, not as const, and it contains a
references type as a member. Then reference member will be modified,
hence there is a need to send even value types as const if such
behavior is desired. Now, please tell me how it's done.
As I said, you can't. C# doesn't have the concept of const arguments for
methods. If a reference type is mutable, then any code with a reference
to an instance can theoretically mutate the instance.

"Theoretically" because, of course, you could use member accessibility as
a way of limiting what code can modify the instance, by making any member
of the class that could mutate the instance inaccessible to the code using
the instance. It's not really the same thing as "const" would be, but
depending on your goals, it could work.

Of course, another approach is to design immutable classes. One very
well-known example of an immutable class is the String class. Immutable
types can be inconvenient in some ways, but they offer some surprisingly
powerful promises that can lead to very elegantly safe code. With an
immutable class, any reference to that class is inherently "const".

Note that even in C++, passing an argument as "const" wouldn't cause
mutable members of that argument to also be "const". That is, a reference
member of a class or struct could itself still be modified, even if you
can't change the reference in the passed argument. There might be a
language that supports that sort of thing, but it's not ones I'm most
familiar with. It shouldn't be surprising that C# doesn't include that
feature.

Pete
Oct 1 '08 #4
PuzzleCracker... As Peter suggest you can use an immutable class.

If you simply want to keep a particular client from modifying the class,
wrap the
mutable class in a read only wrapper so that the client cannot directly
modify
the encapsulated mutable class and pass a reference of type immutable
wrapper
to the client. This is the strong guarantee. The generic collections,
for instance,
have a method AsReadOnly() that returns an object reference that
implements
the read only IList interface.

Finally a kludge is to design an inheritance hierarchy with a read only
base class
and a read write sub class. Create an instance of the read write sub
class and
then create a reference variable of type read only base class. Pass the
reference
of type read only base class to the client. This is the weak guarantee,
since the
client can still cast the read only reference to the read write sub
class type.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Oct 1 '08 #5

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

Similar topics

4
by: JR | last post by:
Hey all, I am passing a two dimensional array to a function. It basically looks like this int test(double array2 , const CONST1, const CONST2) { int returnValue; for (int i = 0;...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: Rafe Culpin | last post by:
Does anyone please know of a way to access static methods of a class, when the name of that class is held in a variable? I have several classes (PHP5) which all have identically named methods and...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
12
by: Bob Altman | last post by:
Hi all, I want to call a function that takes a void* argument and pass it the address of a constant. Is there a way to do this without explicitly defining the constant and then passing the...
8
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
2
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const...
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
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
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
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...
0
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...

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.