如何处理构造函数? [英] How to deal with constructors?
问题描述
我有一个显示构造函数重载的代码..
我的第一个表单Login.cs和代码是。
private void button1_Click( object sender,EventArgs e)
{
this .Hide();
EmpPanel f = new EmpPanel(textBox1.Text);
f.Show();
}
第二表格是EmpPanel.cs我有2个构造函数 -
public EmpPanel( string uid)
{
InitializeComponent();
此 .id1 = uid;
}
public EmpPanel()
{
InitializeComponent();
此 .button1.Enabled = false ;
此 .ControlBox = false ;
}
当我点击它时,第三个表单有一个取消按钮它再次返回到EmpPanel.cs ..
< pre lang =c#> private void button4_Click( object sender,EventArgs e)
{
this .Hide();
EmpPanel ep = new EmpPanel();
ep.Show();
}
问题是,当我点击thirdform的button4然后它初始化EmpPanel.Cs的Id然后记录没有保存在数据库中id而不是它将id保存为0.为什么..
它保存为0,因为您从未指定ID。如果您知道该ID,则调用重载的ID,否则可能会有一个属性允许您在以后知道它时进行设置。
I have a code that shows constructor overloading..
My First form Login.cs and the code is.
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
EmpPanel f = new EmpPanel(textBox1.Text);
f.Show();
}
Second Form is EmpPanel.cs i have 2 constructors--
public EmpPanel( string uid)
{
InitializeComponent();
this.id1 = uid;
}
public EmpPanel()
{
InitializeComponent();
this.button1.Enabled = false;
this.ControlBox = false;
}
Third form is have a button cancel when i click on it it again return to EmpPanel.cs..
private void button4_Click(object sender, EventArgs e)
{
this.Hide();
EmpPanel ep = new EmpPanel();
ep.Show();
}
Problem is that when i click on the button4 of thirdform then it initialize the Id of EmpPanel.Cs then record is not saved in the database with that id instead of it the id is saved as 0. Why..
It is saved as 0 because you never specify the ID. If you know the ID then call the overloaded one otherwise there is likely a property to allow you to set it later on when you do know it.
这篇关于如何处理构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!