constant variable, local and passing to methods

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

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?
 
You can't.



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
 
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
 
Back
Top