Re: Const parameter to function

Tech-Archive recommends: Fix windows errors by optimizing your registry




awheeler@xxxxxxxxxxxxxxxxx wrote:
Is it possible to pass an object reference to a function and ensure
that the function is unable to modify the state of the object?

One clever alternative that was mentioned recently in another thread is
to create an interface for whatever of the object's properties / events
/ methods are required, but not include any setters in the interface
(or any methods that modify the object's state). For example:

public interface IImmutablePerson
{
int Id { get; }
string Name { get; }
}

public class Person : IImmutablePerson
{
private int _id;
private string _name;

public Person(int id, string name) { this._id = id; this._name =
name; }
public int Id { get { return this._id; } set { this._id = value; }
}
public string Name ( get { return this._name; } set { this._name =
value; } }
public void TrimName() { this._name = this._name.Trim(); }
}

public void DoSomethingWithPerson(IImmutablePerson person) { ... }

In this case, you're not _guaranteed_, but are reasonably assured that
DoSomethingWithPerson will not change anything in the Person object,
because it uses it via the interface IImmutablePerson, which doesn't
declare any property setters and doesn't include the method TrimName()
(which modifies the Name).

Now, if DoSomethingWIthPerson chose to be evil, it could always do
this:

Person mutablePerson = (Person)person;
mutablePerson.Name = "foo";

but this would clearly violate the intent of the declaration, which is
that the argument not be modified in any way.

If you truly, truly want pass-by-value at the object state level, with
full guarantees, I would say Clone that puppy, and pass the clone.
(This assumes, of course, that the object in question implements
ICloneable. Even then, the method might be able to modify objects that
are referred to by both the original object and the clone, if there are
any.)

.



Relevant Pages

  • Re: Few beginner questions,pls help
    ... Programs that have duplicated logic are hard to modify. ... Imagine you have an object A which contains a reference to object B, if you copy object A to object C, then you have a new object - object C, but object C still has a reference to object B. This may not be what you require. ... Now imagine that you override object A's clone method & in this method you set it's reference to object B to a new object of the same type as B & copy B's data into it. ... Now you clone object A to object C, then C no longer has a reference to object B, but to a new object with the same data which was in object B. The difference here is that if the copied object C modifies object B this is the SAME object B that object A points to, so both object A & C now have the same modified data in object B. With the cloned object C, if it modifies its NEW referenced object of the same type as B, then it has no effect on object B method), so the objects referenced from A & C now contain different data. ...
    (comp.lang.java)
  • Re: How to insert StudentID from main form into subform?
    ... to modify any fields on that form..then you MUST have some field ... on-insert event will fire). ... ensure the cursor is on a field that can be modified (so, ... check where you have some code that modifies fields/control ...
    (microsoft.public.access.forms)
  • Re: Clone SkinnableUI
    ... I am trying to modify Commctrl part of the OS design in Win CE. ... But similar steps can't be repeated to clone commctrl. ... pointers as to how should we interpret the makefile instruction to ... you'll find that all you need to do is to copy the gcacheviewxp folder ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Clone SkinnableUI
    ... If you just want to modify the skin of commctrl than clone commctrlview ... Read the master makefile at section: ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Dual-Licensing Linux Kernel with GPL V2 and GPL V3
    ... TiVo includes a copy of Linux in its DVR. ... Per this reasoning, nobody never modifies software. ... You may modify your copy or copies of the Program or any portion ...
    (Linux-Kernel)