basvas
basvas

Reputation: 363

listener c# like java

I have field string in struct, and i want learn real-time changed this field.

struct example {
public string ex;
}

examp = new example();<BR>
examp.ex = "test";

////// then program work and eamp.ex = "bing";

I need method

on_ex_changed() 
{
    if examp.ex changed then ..... 
}

online and simple plz

Upvotes: 0

Views: 2160

Answers (1)

Mitzi
Mitzi

Reputation: 2811

You can put an event at the setter as follows. The event will be fired every time the setter is called.

public class MyObj
{
    private RectangleF mRectangle;

    public event EventHandler RectangleChanged;

    public RectangleF Rectangle
    {
        get
        {
            return mRectangle;
        }

        set
        {
            mRectangle = value;
            OnRectangleChanged();
        }
    }

    protected virtual void OnRectangleChanged()
    {
        if (RectangleChanged != null)
        {
            RectangleChanged(this, EventArgs.Empty);
        }
    }
}

Upvotes: 4

Related Questions