views:

71

answers:

2

I created a property

public int PK_ButtonNo 
{
    get { return PK_ButtonNo; }
    set { PK_ButtonNo = value; }
}

Now I want to add events to this property for value changing and changed.

I wrote two events. Here I want both the events to contain changing value as well as changed value.

i.e

When user implements the event. He must have e.OldValue, e.NewValue

public event EventHandler ButtonNumberChanging;
public event EventHandler ButtonNumberChanged;

public int PK_ButtonNo 
{
    get { return PK_ButtonNo; }
    private set
    {
        if (PK_ButtonNo == value)
            return;

        if (ButtonNumberChanging != null)
            this.ButtonNumberChanging(this,null);

        PK_ButtonNo = value;

        if (ButtonNumberChanged != null)
            this.ButtonNumberChanged(this,null);
    }
}

How will I get the changing value and changed value when I implement this event.

+3  A: 

Add the following class to your project:

public class ValueChangingEventArgs : EventArgs
{
    public int OldValue{get;private set;}
    public int NewValue{get;private set;}

    public bool Cancel{get;set;}

    public ValueChangingEventArgs(int OldValue, int NewValue)
    {
        this.OldValue = OldValue;
        this.NewValue = NewValue;
        this.Cancel = false;
    }
}

Now, in your class add the changing event declaration:

public EventHandler<ValueChangingEventArgs> ButtonNumberChanging;

Add the following member (to prevent stackoverflow exception):

private int m_pkButtonNo;

and the property:

public int PK_ButtonNo
{
    get{ return this.m_pkButtonNo; }
    private set
    {
        if (ButtonNumberChanging != null)

        ValueChangingEventArgs vcea = new ValueChangingEventArgs(PK_ButtonNo, value);
        this.ButtonNumberChanging(this, vcea);

        if (!vcea.Cancel)
        {
            this.m_pkButtonNo = value;

            if (ButtonNumberChanged != null)
            this.ButtonNumberChanged(this,EventArgs.Empty);
        }
    }
}

The "Cancel" property will allow the user to cancel the changing operation, this is a standard in a x-ing events, such as "FormClosing", "Validating", etc...

Nissim
You beat me! I was nearly done typing!
Nathan Taylor
Post it! and let the better solution win! (:
Nissim
@Nathan: Yes you can do it. thx to Nissim and all those who made efforts to provide solution.
Shantanu Gupta
@Nissim and @Shantanu I'm not really worried about it, Nissim's solution is verbatim identical.
Nathan Taylor
A: 

Yes, that is correct solution. I would define ValueChangingEventArgs as struct instead of class since it is a "value object".

Petr Kozelek
Nissim
@Nissim: Wow Nice link. +1 for this link too :)
Shantanu Gupta
True, true. In my opinion DTO like this it should be struct. That's all.
Petr Kozelek
@Petr: This cannot be struct for 2 reasons:1) a struct cannot inherit from a class (only from interface, meaning that ValueChangingEventArgs cannot inherit from EventArgs - compilation error)2) If I used struct - the "Cancel" property will have no affect, since the user assigned to the event only changes a copy of the struct, and not the value itself, meaning that "cancel" will always be false
Nissim
Sorry. I did not read carefully and I was talking about ValueChangedEvent. If talking about ValueChangingEvent you are right that the user should have the option to change the value.
Petr Kozelek