checking if a variable has changed

I have a dll that I created that returns an int to the parent control (form)
  public int pc
        {
            get { return _pc; }
            set
            {
                _pc = value;
            }
        }

Open in new window

in the form I just call pcs.pc and I have what was seleced

I also have a public int pc{get;set;} in the form
how can I tell if that variable has changed? while I am in the main form?
LVL 6
r3nderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
you could create your own event and invoke it in the setter. and then have your main form subscribe to that event... make sure you also have an unsubscribe to the event or you could produce a memory leak
0
r3nderAuthor Commented:
not sure how I would do that?
0
p_davisCommented:
public event EventHandler PCChanged(object sender, eventargs e);

 public int pc
        {
            get { return _pc; }
            set
            {
                _pc = value;
if(PCChanged != null)
Invoke.PCChanged(this, new eventargs());
            }
        }


sorry but im doing this without an IDE at this time. but this is pretty close to what it should be. if you need to customize parameters sent then we would need to create a delegate to take the place of eventhandler.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

r3nderAuthor Commented:
I couldn't get it to work
0
Fernando SotoRetiredCommented:
Hi r3nder;

This should do what you are looking for.

public class MyPC
{
    // Starting off with an empty handler avoids pesky null checks
    public event EventHandler PcStateChanged = delegate { };
    private int _pc = 0;

    public MyPC()
    {
        // Add an event handler for the property to report on
        this.PcStateChanged += MyPC_PcStateChanged;
    }

    // The event that fires when _pc changes
    void MyPC_PcStateChanged(object sender, EventArgs e)
    {
        MessageBox.Show("I have changed");
    }

    public int Pc
    {
        get { return _pc; }
        set
        {
            // check to see if the new value is the same
            // as the old one if no change do not raise the event 
            // or assign the value
            if (_pc == value)
            {
                return;
            }

            _pc = value;
            PcStateChanged(this, EventArgs.Empty);
        }
    }

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
p_davisCommented:
Thanks for the follow up fernando... i was offline all night.
0
r3nderAuthor Commented:
Thanks guys, Fernando, thanks for the easy to follow explanation. It was very much appreciated :D
0
Fernando SotoRetiredCommented:
Not a problem, Always glad to lend a hand.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.