变量改变触发事件时提示“未将对象引用设置到对象的实例”

.Net技术 码拜 9年前 (2015-11-16) 980次浏览
前几天在这上面提问,根据网友们的回答本人编了一个变量改变的时候触发窗体的标签改变的程序,遇到了新问题,请各位高手再帮本人看看!
事件类,与初始值比较,判断Temp的值能否改变,改变则触发

public class NetStateEvent
    {
        public delegate void tempChange(object sender, EventArgs e);
        public event tempChange OntempChange;
        bool temp = false;
        public bool Temp
        {
            get { return temp; }
            set
            {
                if (temp != value)
                {
                    OntempChange(this, new EventArgs());
                }
                temp = value;
            }
        }
    }

触发源,为了测试,当程序运行10s后改变Temp的值

public Test
{
      NetStateEvent ven = new NetStateEvent();
      if(time = 10)
      {
             ven.Temp = true;
       }
}

以上两部分代码在同一个项目(类库)中,下面的事件使用代码在窗体项目中

public partial class FendClient : Form
{
    NetStateEvent ven = new NetStateEvent();
     public FendClient()
        {
             ven.OntempChange += new NetStateEvent.tempChange(ven_OntempChange);
             InitializeComponent();
        } 
         public void ven_OntempChange(object sender, EventArgs e)
        {
             处理代码。
        }
}

到10s的时候,出现下面错误
变量改变触发事件时提示“未将对象引用设置到对象的实例”

解决方案:10分
本人都怀疑你,ven.Temp = true; 能否和FendClient内的是同一个对象了
解决方案:5分
首先,在触发事件前,应事先检查事件能否为null;然后,你的事件源是在类库中,然后订阅事件在form中,这没有冲突,只要保证两个事件源对象是同一个对象就行了,这样就不会出现订阅了一个对象的事件,而在触发另一个对象的事件的问题。
解决方案:5分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class NetStateEvent
    {
        public delegate void tempChange(object sender, EventArgs e);
        public event tempChange OntempChange;
        bool temp = false;
        public bool Temp
        {
            get { return temp; }
            set
            {
                if (temp != value)
                {
                    if(OntempChange!=null)
                           {  OntempChange(this, new EventArgs());}
                }
                temp = value;
            }
        }
    }
解决方案:20分
//给你一个完整的代码。

using System;
using System.Windows.Forms;
using System.Threading;
namespace 事件01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        eventTest t = new eventTest();  //建一个对象
        private void Form1_Load(object sender, EventArgs e)
        {            
            t.OntempChange += t_OntempChange; //注册事件
        }
        //窗体上拖了个标签,当事件发生时改变其文本内容
        void t_OntempChange()  //事件处理代码
        {
            label1.Text = "变量已经改变。";
        }
        //窗体上拖了个按钮,这个按钮来启动事件引发类
        private void button1_Click(object sender, EventArgs e)
        {
            t.run();
        }
    }
    public class eventTest  //事件引发类
    {
        bool temp = false;  //要监控的变量 
        public delegate void tempChange(); //定义委托
        public event tempChange OntempChange; //定义事件
        public bool Temp   
        {
            get { return temp; }
            set
            {   //对属性的设置可能引发事件
                if (temp != value)
                {
                    OntempChange();
                }
                temp = value;
            }
        }
        //本对象的运行代码,其中有一条是改变属性的
        public void run()
        {
            //………其它代码……………
            //运行5秒后本人来引发事件吧
            Thread.Sleep(5000);
            Temp = true;
        }
    }
}
解决方案:5分
if(OntempChange!=null)
{
OntempChange(this, new EventArgs());
}
OntempChange并不是一定会被初始化的,
就像你窗体上,并不是全部的按钮都有OnClick事件
解决方案:5分
所以才要判断一下能否为null,假如是null,就没有被初始化,就不用调用了,假如不为null,才会触发

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明变量改变触发事件时提示“未将对象引用设置到对象的实例”
喜欢 (0)
[1034331897@qq.com]
分享 (0)