| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 736 人关注过本帖
标题:新手求助 网上刚看到的C#的四个基本技巧
只看楼主 加入收藏
zxg6652330
Rank: 2
等 级:论坛游民
威 望:1
帖 子:17
专家分:79
注 册:2012-6-12
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
新手求助 网上刚看到的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
lhb62232397
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:117
专家分:546
注 册:2011-11-6
收藏
得分:10 
因为没有看到你的全部代码,所以不能确定是什么。
但是根据经验大致判断:OnGetLine很有可能是自定义类
所以,你自己好好看看前后逻辑关系
2012-06-12 17:23
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
快速回复:新手求助 网上刚看到的C#的四个基本技巧
数据加载中...
 
   



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

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