如何处理构造函数? [英] How to deal with constructors?

查看:98
本文介绍了如何处理构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示构造函数重载的代码..



我的第一个表单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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆