Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everybody,
I have a string type variable 'card_serialNo"; I want to perform certain functions when it value get changed. please inform me if I can make a value change event for this type of variable.
thank You.
Posted

You can create a property for this variable and implement INotifyPropertyChanged interface and do this.
Or else, you can create a property for this variable and call a method from the set block of the property.
 
Share this answer
 
If you just need this internally in the class, then either create a SetCardSerial method or create a property and use the setter.

If you want an event that is accessible outside the class then you will need something like this:
public class YourClass
{
    public event EventHandler CardSerialNumberChanged;

    private string cardSerialNo;

    public string CardSerialNo
    {
        get
        {
            if (cardSerialNo == null)
                return string.Empty;
            else
                return cardSerialNo;
        }
        set
        {
            if (value == string.Empty)
                value = null;
            if (cardSerialNo != value)
            {
                cardSerialNo = value;
                OnCardSerialNumberChanged(EventArgs.Empty);
            }
        }
    }

    protected virtual void OnCardSerialNumberChanged(EventArgs e)
    {
        EventHandler eh = CardSerialNumberChanged;
        if (eh != null) eh(this, e);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900