tags:

views:

169

answers:

4
class SimpleDelegate
    {
        public delegate void LogHandler(string message);

        public void Process(LogHandler logHandler)
        {
            if (logHandler != null)
            {
                Console.WriteLine("Process begin");
            }

            if (logHandler != null)
            {
                Console.WriteLine("Process end");
            }
        }
    }

    class FileLogger
    {
        FileStream fileStream;
        StreamWriter writer;

        public FileLogger(string fileName)
        {
            fileStream = new FileStream(fileName, FileMode.Create);
            writer = new StreamWriter(fileStream);
        }

        public void Logger(string s)
        {
            writer.WriteLine(s);
        }

        public void Close()
        {
            writer.Close();
            fileStream.Close();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SimpleDelegate cp = new SimpleDelegate();

            FileLogger fl = new FileLogger(@"C:\TEMP\MyLog.log");

            SimpleDelegate.LogHandler handler = null;
            handler += new SimpleDelegate.LogHandler(Logger);
            handler += new SimpleDelegate.LogHandler(fl.Logger);

            cp.Process(handler);

            fl.Close();

        }

        static void Logger(string s)
        {
            Console.WriteLine("writing s " + s);
        }
    }

On executing I get following output: Process begin Process end Press any key to continue . . .

Actually it should be, Process begin Process end Process begin Process end Press any key to continue . . .

It's week, I am not able to figure out the mistake :(

A: 

I think this is what is happening

    SimpleDelegate cp = new SimpleDelegate();

    FileLogger fl = new FileLogger(@"C:\TEMP\MyLog.log");

    cp.Process(null);
    fl.Close();

That is handler is null, so that code never gets executed. You see your message once because of the call to cp.Process(null)

Bob
thanks for answering.
Saar
+4  A: 

You're only calling SimpleDelegate.Process once - why would you expect to see output twice?

You're never actually invoking the delegate... just testing it for nullity. You're testing that twice, once before writing "Process begin" and once before writing "Process end", but that's all.

Jon Skeet
thanks. got the point. :)
Saar
Thanks again. Now I call Invoke(string s) inside cp.Process, and output is as expected as line at Console and same line in MyLog.log file too. :)
Saar
+4  A: 

I'm not sure if I'm getting the point ...

But you never call the logHandler in this code:

    public void Process(LogHandler logHandler)
    {
        if (logHandler != null)
        {
            Console.WriteLine("Process begin");
        }

        if (logHandler != null)
        {
            Console.WriteLine("Process end");
        }
    }
Stefan Steinegger
+1: Well spotted :)
leppie
+1  A: 

I believe your confusion is around the fact that you called "LogHandler += ..." twice, but are only getting two outputs. Notice that the Process function you are calling doesn't do a loop that uses the LogHandler! That's why you're not getting the results you expect.

Also, the model you are using is just slightly off. Instead of directly using the delegate, it is better to create an event and subscribe to that.

public delegate void LogHandler(string message);
public event LogHandler OnLog;
...
OnLog += ...
John Fisher
thanks for the hint. I will try with event.
Saar