Hi Tryst,I think it may be a good idea to implement the Observer pattern here,since your clsBarCodeScanner should not need to know as to which method to call on the FormSnagList form.The approach should be that FormSnagList (or anybody else)should register its interest with the clsBarCodeScanner and whenever the state of clsBarCodeScanner changes,it will notify all objects who have interest.
Here is the code-
//Any object which wants other object to register its interest with itself will implement this interface
interface IObservable
{
void AddObserver(IObserver observer);
void Notify();
}
//Any object which wants to update itself when notified by the subject will implement this interface
interface IObserver
{
void Update();
}
class clsBarcodeScanner : IObservable //Allows objects to register itself
{
// Declare and initialise EventsHandlers.
private System.EventHandler MyEventHandler = null;
private ArrayList list = new ArrayList();
public void AddObserver(IObserver observer) //Add observers
{
list.Add(observer);
}
private bool InitReader()
{
// Create event handler delegate
this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
return true;
}
private void MyReader_ReadNotify(object sender, EventArgs e)
{
// If it is a successful read (as opposed to a failed one)
if (MyReaderData.Result == Symbol.Results.SUCCESS)
{
Notify(); //Notify all observers
// WHAT TO CALL THE FORM METHOD HERE!
}
}
#region
IObservable Members
public void Notify()
{
foreach (IObserver observer in list)
observer.Update(); //Loop through each observer asking it to update itself
}
// Form Class
public class FormSnagList : System.Windows.Forms.Form, IObserver
{
private clsBarcodeScanner m_clsBarcodeScanner;
public FormSnagList()
{
m_clsBarcodeScanner =
new clsBarcodeScanner();
m_clsBarcodeScanner.AddObserver(
this); //Add self to the clsBarCodeScanner
}
public void MethodWantToCall()
{
// THE METHOD I WANT TO CALL.
}
#region
IObserver Members
void IObserver.Update()
{
MethodWantToCall(); //Call method when notified
}
#endregion
}
This achieves high level of decoupling and importantly,the observer itself can serve as an observable later.