tags:

views:

55

answers:

4

If I have a class that contains some properties and methods, and when a property is changed, it fires an event (in this case passing a string in the args):

public class Settings
{
    public delegate void SettingsChangedHandler(object sender, string e);
    public event SettingsChangedHandler SettingsChanged;

    private string rootfolder;
    public string RootFolder
    {
        get { return rootfolder; }
        set
        {
            rootfolder = value;
            if (SettingsChanged != null)
                SettingsChanged(this, "ROOT_FOLDER");
        }
    }
}

If i have somewhere in my code:

public Settings SettingsInstance = new Settings();
SettingsInstance.SettingsChanged += new SettingsChangedHandler(SettingsInstance_SettingsChanged);
SettingsInstance = SomeOtherSettingsInstance;

I want all of the properties that have changed to fire their events.

How do I achieve something like this? Surely I don't have to copy them over one at a time?

A: 

Why not just perform the initialization the other way around?

Settings SettingsInstance = SomeOtherSettingsInstance;
SettingsInstance.SettingsChanged += new SettingsChangedHandler(SettingsInstance_SettingsChanged);

The way you are currently performing the assignment you will overwrite your instance, SettingsInstance, where you just configure the SettingsChanged event.

I believe you would still need to copy everything manually to make sure all of the fields on the new instance are correct. You might be able to get by with a shallow copy using Object.MemberwiseClone. For a more in-depth discussion of a Shallow Copy vs. Deep Copy see this wikipedia link.

smaclell
MemberwiseClone copies fields directly, so it would bypass the code that raises SettingsChanged.
itowlson
You are right! mmm I am kinda confused by the question then ...
smaclell
A: 

This is because properties are not changing, you are just reassigning references.

n535
+1  A: 

This line of code:

SettingsInstance = SomeOtherSettingsInstance;

does not copy anything inside the objects, instead it overwrites the reference stored in SettingsInstance with the reference stored in SomeOtherSettingsInstance.

The object itself is none the wiser.

Basically, after you have executed the first of the 3 last lines, you have this scenario:

SomeOtherSettingsInstance -----> Object 1 in memory of type Settings

SettingsInstance --------------> Object 2 in memory of type Settings
                        ^
                        |
                        +- References

After you've executed the third line, this is how it looks:

SomeOtherSettingsInstance --+--> Object 1 in memory of type Settings
                           /
SettingsInstance ---------+      Object 2 in memory of type Settings

Now you have two references to the first object, one through each variable, and you've left the new object you just created to rot for the garbage collector to come pick it up later.

If you wish to copy the internals, then yes, you have to copy one property at a time.

I regularly create cloning support like this:

public Settings Clone()
{
    Settings clone = CreateCloneInstance();
    CloneTo(clone);
    return clone;
}

protected virtual Settings CreateCloneInstance()
{
    return new Settings();
}

public virtual void CloneTo(Settings clone)
{
    clone.RootFolder = RootFolder;
    ... + any other properties you might have
}

In your scenario, you want to hook up an event before copying things, so you would call it like this:

public Settings SettingsInstance = new Settings();
SettingsInstance.SettingsChanged += SettingsInstance_SettingsChanged;
SomeOtherSettingsInstance.CloneTo(SettingsInstance);

The reason I implement cloning support like that is due to object hierarchies. If that is not an issue for you (you're not going to inherit from Settings), you can just do this:

public Settings Clone()
{
    Settings clone = new Settings();
    CloneTo(clone);
    return clone;
}

public void CloneTo(Settings clone)
{
    clone.RootFolder = RootFolder;
    ... + any other properties you might have
}
Lasse V. Karlsen
Since the event handler is set before the cloning takes place the event will fire every time a property is set.
smaclell
ok thanks i will have to do it one property at a time then
Dave
A: 

As Lasse pointed out, assignment to a reference variable just changes what object that variable refers to, and does nothing to the object.

The meaning of assignment is rigidly controlled by the C# compiler. You can override it on a property, but not on a local variable. So the closest you can get to a pattern for this is:

interface IWantAssignmentNotification
{
    void LostAssignment();
    void GainedAssignment();
}

class Ref<T> where T : IWantAssignmentNotification
{
    private T _value;

    public T Value 
    {
        get { return _value; }
        set
        {
            if (_value != null)
                _value.LostAssignment();

            _value = value;

            if (_value != null)
                _value.GainedAssignment();                
        }
    }
}

Now your Settings class has to implement IWantAssignmentNotification, and you can use Ref<Settings> to hold a reference to it:

Ref<Settings> refSettings = new Ref<Settings> { Value = new Settings() };

refSettings.Value = someOtherSettingsInstance;

The first line will call GainedAssignment on the new instance of Settings. The second line will call LostAssignment on that instance followed by GainedAssignment on the other instance. The idea being that you would make Settings fire certain events in either or both of those.

But of course, this doesn't stop the erroneous:

refSettings = new Ref<Settings> { Value = someOtherSettingsInstance };

That simply discards the old Ref<T> object, and so no one ever tells the previous Settings instance that it is no longer assigned to a "live" variable.

Daniel Earwicker