标题:新手求助 网上刚看到的C#的四个基本技巧
取消只看楼主
zxg6652330
Rank: 2
等 级:论坛游民
威 望:1
帖 子:17
专家分:79
注 册:2012-6-12
结帖率:100%
已结贴  问题点数:10 回复次数:3 
新手求助 网上刚看到的C#的四个基本技巧
public class KeyboardProcessor
  {
  private OnGetLine theFunc = null; //OnGetLine 这个是什么类型的???  
  public OnGetLine OnGetLineCallback {
  get {
  return theFunc;
  }
  set {
  theFunc = value;
  }
  }
  public void Run (){
  // Read input.
  // If there is any listeners, publish:
  string s;
  do {
  s = Console.ReadLine ();
  if (s.Length == 0)
  break;
  if (theFunc != null){
  System.Delegate [] funcs =theFunc.GetInvocationList();
  foreach (OnGetLine f in funcs) {
  try {
  f (s);
  } catch (Exception e) {
  Console.WriteLine
  ("Caught Exception: ", e.Message);
  }
  }
  }
  } while (true);
搜索更多相关主题的帖子: null private public return 
2012-06-12 16:52
zxg6652330
Rank: 2
等 级:论坛游民
威 望:1
帖 子:17
专家分:79
注 册:2012-6-12
得分:0 
这个是我从网上看到的  一直纠结这个OnGetLine  
后来这么定义了一下public delegate void OnGetLine(string strFuncName);
  static void Main(string[] args)
     {
                    KeyboardProcessor kp = new KeyboardProcessor();
                    kp.Run();
                    Console.ReadKey();
     }
之后theFunc 总是null  不知道我那个环节出了问题?
2012-06-12 17:32
zxg6652330
Rank: 2
等 级:论坛游民
威 望:1
帖 子:17
专家分:79
注 册:2012-6-12
得分:0 
问题已解决,谢谢2楼的提议。
原来是少方法的传入。
2012-06-13 10:05
zxg6652330
Rank: 2
等 级:论坛游民
威 望:1
帖 子:17
专家分:79
注 册:2012-6-12
得分:0 
把我调试可以通过的写下 共享下。。  不对的地方 大家请指出来
            public static void FUN1(string strFuncName){
                Console.WriteLine("Caught Exception1: {0}", strFuncName);
            }
            public static void FUN2(string strFuncName)
            {
                Console.WriteLine("Caught Exception2: {0}", strFuncName);
            }
            public delegate void OnGetLine(string strFuncName);
            public class KeyboardProcessor
            {
                //private  OnGetLine theFunc=null;
                private OnGetLine theFunc = null;
                public OnGetLine OnGetLineCallback
                {
                    get
                    {
                        return theFunc;
                    }
                    set
                    {
                        theFunc = value;
                    }
                }
                public void Run()
                {
                    // Read input.
                    // If there is any listeners, publish:
                    string s;
                    do
                    {
                        s = Console.ReadLine();
                        if (s.Length == 0)
                            break;
                        if (theFunc != null)
                        {
                            System.Delegate[] funcs = theFunc.GetInvocationList();
                            foreach (OnGetLine f in funcs)
                            {
                                try
                                {
                                    f(s);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Caught Exception: {0}", e.Message);
                                }
                            }
                        }
                    } while (true);
                }
                static void Main(string[] args)
                {
                    KeyboardProcessor kp = new KeyboardProcessor();
                    OnGetLine allfun = null;
                    allfun += FUN1;
                    allfun += FUN2;
                    kp.theFunc = allfun;
                    kp.Run();
                    Console.ReadKey();
                }


            }
2012-06-18 14:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-371398-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048369 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved