-1

I have the following problem: I have a class called Fileand I want this class to inform another class when something special happened, but File should not have a reference to the class which should be informed. I think this is the same problem which the INotifyPropertyChanged-Interface solves. My only workaround is a static list which contains the instances of the class which should be informed and a static method of this class, but I think this is not the smartest way to do it. So, which concept does PropertyChanged use?

Edit:

Lets assume that the class which should react (be informed) is called FileManager, has an implicit event called FinishedReading and this class is only instantiated one time. How can I bind the FileManagers FinishedReading-Event to the method FinishReading() of the class File, if File should not have any reference to FileManagers instance? I already tried to pass the FileManagers Event as parameter, but it didn`t work.

Example:

    [global::Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
    public class FileManager
    {
        public delegate void MyDelegate();
        public event MyDelegate FinishedReading;
    }

    [global::Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
    public class File
    {
        // Whats the exact Data-Type of the FinishedReading-Event?
        public File(FileManager.MyDelegate eventInstance)
        {
            eventInstance += FinishReading;
        }

        public void FinishReading()
        {
            //
        }
    }
Feve123
  • 122
  • 11
  • Start reading here: [Events (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/awbftdfh.aspx). – Clemens Oct 29 '14 at 08:49
  • @Clemens habe meine Frage editiert, wie löse ich mein Problem? Edited my question, how can I solve this problem? – Feve123 Oct 29 '14 at 11:12

1 Answers1

0

In this case i would work with events, not with some static methods. INotifyPropertyChanged is a kind of listener system. The WPF-Systems check wether your binded class inhert from INotifyPropertyChanged and call the method PropertyChanged when needed.

First solution:

Ok, in this case i would make FileManage a singleton, but you only have one instance. An in the File you can call FileManage.Singleton.FinishReading() or something like that.

public class FileManager
{
    private static readonly FileManager singleton = new FileManager();

    public static FileManager Singleton
    {
        get { return FileManager.singleton; }
    } 


    private FileManager()
    { 

    }

    public void FinishReading()
    { 

    }
}

public class File
{
    public void FinishReading()
    {
        //
        FileManager.Singleton.FinishReading();
    }
}

Second solution:

You can use your FileManager class as a factory for File and set a delegate to FinishReading(). Than you have a nice interface for create Files and don't have to care about passing the right parameter.

public class FileManager
{
    public FileManager()
    { 

    }

    public File CreateFile()
    {
        File f = new File(this.FinishReading);

        return f;
    }

    public void FinishReading()
    { 

    }
}

public class File
{
    public delegate void FinishReadingDelegate();
    private FinishReadingDelegate del;

    public File(FinishReadingDelegate Del)
    {
        del = Del;
    }


    public void FinishReading()
    {
        //
        del.Invoke();
    }
}
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Danke dir! Ich verwende das Singleton-Muster, ist für mich zwar auch neu, dennoch "einfacher" als das Factory-Konzept. Btw, lernst du C# an der Uni? English: Thank you. – Feve123 Oct 29 '14 at 15:17
  • @Feve123 i' m from germany too, but i would recommend that you write in english :) It is the best if you learn it on your own, maybe try and error. I'm working with C# since 2005. – BendEg Oct 29 '14 at 21:49