Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace evnt
{
    public class myclass
    {
        public delegate void LogHandler(string msg);
        public event LogHandler Log;
        public void process()
        {
            OnLog("Process() begin");
            OnLog("Process() end");
        }
        protected void OnLog(string message)
        {
            if (Log != null)
            {
                Log(message);
            }
        }
    }
    public class FileLogger
    {
        FileStream filestream;
        StreamWriter streamwriter;
        public FileLogger(string filename)
        {
            filestream = new FileStream(filename,FileMode.Create);
            streamwriter = new StreamWriter(filestream);
        }
        public void Logger(string s)
        {
            streamwriter.WriteLine(s);
        }
        public void close()
        {
            streamwriter.Close();
            filestream.Close();
        }
    }
    public class testapplication
    {
        static void Logger(string s)
        {
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FileLogger f1 = new FileLogger("process.log");
            myclass mc = new myclass();
            mc.Log += new myclass.LogHandler(Logger);
            mc.Log += new myclass.LogHandler(f1.Logger);
            mc.process();
            f1.close();
        }
    }
}

there is error in main function 3rd line of code ,and parameter that pass Logger.
error: 'Logger' does not exist in current context.

What I have tried:

I tried to ask that what is solution of this code, and what is my mistake for it, any conceptually? i want to clear this concept and learn more regard this.
Posted
Updated 9-Mar-16 18:14pm
v2

Your mistake: You tried to access "Logger()" from your Main method and the Main method did not have access to it because "Logger()" is defined in the FileLogger class.

To clear your concept probably you need to understand the pillars of object oriented programming especially, the first one, "Encapsulation".
 
Share this answer
 
v2
Comments
Member 11572517 9-Mar-16 23:58pm    
sir , i knew concept of oops and encapsulation.thanks for it
i solved this error.
i forgot that in testapplication class Logger method putting of console.writeline(s); parameter, and in main function : i create one object of ts as testapplication class and then through object i call that logger.
mc.log += new myclass.loghandler(ts.Logger);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900