查询c#事件简单代码 [英] Query regard of c# event simple code

查看:104
本文介绍了查询c#事件简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.IO;

namespace evnt
{
public class myclass
{
public delegate void LogHandler(string msg);
公共事件LogHandler Log;
public void process()
{
OnLog(Process()begin);
OnLog(Process()end);
}
protected void OnLog(字符串消息)
{
if(Log!= null)
{
Log(message);
}
}
}
公共类FileLogger
{
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();
}
}
公共类testapplication
{
static void Logger(string s)
{
Console.WriteLine();
}
}
class程序
{
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();
}
}
}



主函数第3行代码和传递Logger的参数出错。

错误:当前环境中不存在记录器。



我尝试过:



我试图问这个代码的解决方案是什么,我的错误是什么,概念上是什么?我想清除这个概念,并且更多地考虑这个。

解决方案

你的错误:你试图从你的记录器()访问主方法和Main方法无法访问它,因为 Logger() FileLogger 类中定义。



要清除你的概念,你可能需要理解面向对象编程的支柱,尤其是第一个,封装

我解决了这个错误。

i忘记了在testapplication类的logger方法放入console.writeline(s);参数,并在main函数中:我创建一个ts对象作为testapplication类,然后通过对象我调用该记录器。

mc.log + = new myclass.loghandler(ts.Logger);

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.

解决方案

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".


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


这篇关于查询c#事件简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆