private void button1_Click(object sender, EventArgs e)
{
Studen stu = new Studen();//实例化了一个对象
stu.BanJi = banjiin.Text;//赋值
MessageBox.Show(“保存成功”);
}
private void button2_Click(object sender, EventArgs e)
{
banjiout.Text = stu.Banji;//我想这在里使用上面button1的实例化对象,有什么方法么?
}
—- 20分
在这两个方法的外面写
Studen stu;
Studen stu = new Studen();//实例化了一个对象
这个写成
stu = new Studen();//实例化了一个对象
—- 10分
定义一个类的私有字段来保存这个学生实例。
—- 10分
Studen stu = new Studen();//实例化了一个对象
private void button1_Click(object sender, EventArgs e)
{
stu.BanJi = banjiin.Text;//赋值
MessageBox.Show(“保存成功”);
}
private void button2_Click(object sender, EventArgs e)
{
banjiout.Text = stu.Banji;//我想这在里使用上面button1的实例化对象,有什么方法么?
}
—-
实例化对象传递值的示例
//创建自定义公共类
public class SqlData
{
#region 类中的全局变量
public SqlConnection sqlcon; //申明一个SqlConnection对象
private SqlCommand sqlcom; //申明一个SqlCommand对象
private SqlDataAdapter sqldata; //申明一个SqlDataAdapter对象
// 构造函数,初始化时连接数据库
public SqlData()
{
sqlcon = new SqlConnection(ConfigurationManager.AppSettings[“conStr”]);
}
// 此方法用来执行SQL语句
// <param name=”SqlCom”>要执行的SQL语句</param>
public bool ExceSQL(string strSqlCom)
{
SqlCommand sqlcom = new SqlCommand(strSqlCom, sqlcon);
try
{
//判断数据库是否为连连状态
if (sqlcon.State == System.Data.ConnectionState.Closed)
{ sqlcon.Open();}
//执行SQL语句
sqlcom.ExecuteNonQuery();
//SQL语句执行成功,返回true值
return true;
}
catch
{
//SQL语句执行失败,返回false值
return false;
}
finally
{
//关闭数据库连接
sqlcon.Close();
}
}
#endregion
#region 返回SqlDataReader类型的数据
}//codego.net/tags/11/1/
//实例化自定义类给对象赋值修改数据库信息
protected void btnOK_Click(object sender, EventArgs e)
{
SqlData da = new SqlData();
string P_str_Com = “update tbUser set UserName='” + this.txtName.Text + “‘,PassWord='” + this.txtOKpwd.Text + “‘”
+ “,Sex='” + this.ddlSex.SelectedValue + “‘,ReallyName='” + this.txtRealName.Text + “‘”
+ “,Birthday='” + this.txtBirthday.Text + “‘,Email='” + this.txtEmail.Text + “‘,HomePhone='” + this.txtHphone.Text + “‘”
+ “,MobilePhone='” + this.txtMphone.Text + “‘,PostCode='” + this.txtPostCode.Text + “‘,QQ='” + this.txtQQ.Text + “‘”
+ “,ICQ='” + this.txtICQ.Text + “‘,Address='” + this.txtAddress.Text + “‘,Answer='” + this.txtAns.Text + “‘”
+ “,Subject='”+this.txtSubject.Text+”‘,Question='” + this.txtQues.Text + “‘”
+ ” where UserID='” + Request[“UserID”] + “‘”;
bool add = da.ExceSQL(P_str_Com);
if (add == true)
{
Response.Write(“<script language=javascript>alert(‘信息修改成功!’);location=’Index.aspx'</script>”);
}
else
{
Response.Write(“<script language=javascript>alert(‘信息修改失败!’);location=’javascript:history.go(-1)'</script>”);
}
}
—-
private Studen stu;
private void button1_Click(object sender, EventArgs e)
{
this.stu = new Studen();//实例化了一个对象
this.stu.BanJi = banjiin.Text;//赋值
MessageBox.Show(“保存成功”);
}
private void button2_Click(object sender, EventArgs e)
{
banjiout.Text = this.stu.Banji;//我想这在里使用上面button1的实例化对象,通过类成员实现呗
}
—-
引用 楼主 csl950212 的回复:
private void button1_Click(object sender, EventArgs e)
{
Studen stu = new Studen();//实例化了一个对象
stu.BanJi = banjiin.Text;//赋值
MessageBox.Show(“保存成功”);
}
private void button2_Click(object sender, EventArgs e)
{
banjiout.Text = stu.Banji;//我想这在里使用上面button1的实例化对象,有什么方法么?
}
最简单的做法,把Studen stu 声明为全局变量。
—-
public partial class Form4 : Form
{
public Studen stu;
private void button1_Click(object sender, EventArgs e)
{
stu = new Studen();
stu.BanJi = textBox1.Text;
MessageBox.Show(“OK”);
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = stu.BanJi;
MessageBox.Show(textBox2.Text);
}
}
public class Studen
{
public string Name { get; set; }
public string BanJi { get; set; }
}
已测试
—-
引用 7 楼 kongwei521 的回复:
public partial class Form4 : Form
{
public Studen stu;
private void button1_Click(object sender, EventArgs e)
{
stu = new Studen();
stu.BanJi = textBox1.Text;
MessageBox.Show(“OK”);
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = stu.BanJi;
MessageBox.Show(textBox2.Text);
}
}
public class Studen
{
public string Name { get; set; }
public string BanJi { get; set; }
}
已测试