I apologize in advance for a noob question. I have a string which is changing without my control (another party's string) lets call it firstString, every time this string changes I need to carry out an action in my program.
so I made a class that implements the "INotifyPropertyChanged" Interface, and created a string in it with a property, lets call it secondString, and on the main method of the form I've created a "PropertyChangedEventHandler" and an event method which shows a message box with the value of firstString.
everything works well if I manually test and change firstString by clicking a button to change its value and I get a message box with firstString's value after it went through secondString's Property, I set it like this:
SecondString
(this is a property) = firstString;
but the thing is firstString is changing by itself, and I don't have control over it, so if it is set by code to equal secondString's property what happens is that it only works for the first time that it runs.
so now every time secondString's property is changing, the event fires and that part is working OK. but I need to set secondString's value with firstString's value automatically every time that firstString's value changes. and I kind of figure that INotifyPropertyChanged should have somehow worked here for this part as well but I can't understand how. so I was trying to figure our how to "bind" string's A value to secondString's property, and got into DataBinding, but I couldn't find any example to bind two strings together, only about binding to or from a control.
EDIT: here is a code to demo, I think the key that I've missed to note is that firstString is a string I get by another party's class library.
Using AnotherPartyLibrary;
FirstClass fc;
public Form1()
{
InitializeComponent();
fc = new FirstClass();
fc.PropertyChanged += new PropertyChangedEventHandler(fc_PropertyChanged);
fc.SecondString = AnotherPartyLibrary.firstString;
this.Disposed += new EventHandler(Form1_Disposed);
}
void fc_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("Something changed!" + e.PropertyName);
}
public class firstClass :INotifyPropertyChanged
{
private string secondString = string.Empty;
public string SecondString
{
get { return this.secondString; }
set
{
this.secondString = value;
NotifyPropertyChanged("SecondString");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
How is this problem is usually solved? Many Thanks in advance!
Edit: can anybody offer another solution other than what a.azemia has offered?Thanks again!
Ray.