Click here to Skip to main content
14,240,716 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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".
   
v2
Comments
Member 11572517 9-Mar-16 23:58pm
   
sir , i knew concept of oops and encapsulation.thanks for it
Rate this:
Please Sign up or sign in to vote.

Solution 2

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);
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100