已有56人关注
按书上做的出错{ 未将对象引用设置到对象的实例。 }
发表在C#图书答疑 2009-06-22
是否精华
版块置顶:
<<c#从入门到精通>>第526页代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace PWMS
{
    public partial class F_Login : Form
    {
        DataClass.MyMeans MyClass = new PWMS.DataClass.MyMeans();
        public F_Login()
        {
            InitializeComponent();
        }
        private void butClose_Click(object sender, EventArgs e)
        {
            if ((int)(this.Tag) == 1)
            {
                DataClass.MyMeans.Login_n = 3;
                Application.Exit();
            }
            else
                if ((int)(this.Tag) == 2)
                    this.Close();
        }

        private void butLogin_Click(object sender, EventArgs e)
        {
            if(textName.Text!="" & textPass.Text !="")
            {
               //用自定义getcom()在tb_Login数据表中查找是否有当前登录用户
                SqlDataReader temDR = MyClass.getcom("Select*from tb_Login where Name ='"+textName.Text.Trim()+"'and pass='"+textPass.Text.Trim()+"'");
                bool ifcom = temDR.Read();//用read()方法读取数据
               // 当有记录表示
                if(ifcom)
                {
                    //用户名记录到公共变量中
                    DataClass.MyMeans.Login_Name = textName.Text.Trim();

                    //获取当前操作员编号
                    DataClass.MyMeans.Login_ID = temDR.GetString(0);

                    DataClass.MyMeans.My_con.Close();
                    DataClass.MyMeans.My_con.Dispose();
                    
                    //记录当前窗体的Tag属性值
                 
                    [font color=#FF0000]DataClass.MyMeans.Login_n = (int)(this.Tag);//这里出错可是我看了一下和书上一样
[/font]
                    this.Close();
                }
                else
                {
                    MessageBox.Show("密码或用户名错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    textName.Text= "";
                    textPass.Text = "";
                }
            }
            else
            {
            MessageBox.Show("请将登录信息填写完整!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }     
        }
        //按enter时移动鼠标焦点
        private void textName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                textPass.Focus();
        }

        private void textPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                butLogin.Focus();
        }       
    }
}

出错的是: DataClass.MyMeans.Login_n = (int)(this.Tag);说什么这句:{"未将对象引用设置到对象的实例。"}
可是我和书上的一样,而且在MyMeans公共类中也是定义了静态:  public static int Login_n =0;
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2009-06-22
沙发
读者朋友:
    您好,Login_n这个变量在程序中的别的地方进行过设置,你检查一下,好好看一下我们光盘中的源程序。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照