Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体“;名称X在当前上下文中不存在;_C#_Windowsformsintegration - Fatal编程技术网

C# Windows窗体“;名称X在当前上下文中不存在;

C# Windows窗体“;名称X在当前上下文中不存在;,c#,windowsformsintegration,C#,Windowsformsintegration,首先,这是我在这里的第一篇帖子,很抱歉格式不好,雅达雅达雅达。现在,我正在为我的大学课程学习C#,我对一些基本的windows窗体和C#逻辑有一个问题,如果你能帮助我,我将非常感激! 所以,我创建了我的表单,它有两个文本框和一个按钮。按下按钮后,我希望程序为列表创建一个新的类成员。(我想我最后的解释有点错误,所以我将把我的代码链接到这里)。因此,基本上我们开始: 主程序: namespace AddPersonTest { public static class Program

首先,这是我在这里的第一篇帖子,很抱歉格式不好,雅达雅达雅达。现在,我正在为我的大学课程学习C#,我对一些基本的windows窗体和C#逻辑有一个问题,如果你能帮助我,我将非常感激! 所以,我创建了我的表单,它有两个文本框和一个按钮。按下按钮后,我希望程序为列表创建一个新的类成员。(我想我最后的解释有点错误,所以我将把我的代码链接到这里)。因此,基本上我们开始: 主程序:

    namespace AddPersonTest
{
    public static class Program
    {

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            List<Person> Persoane = new List<Person>();
        }
    }
}
按钮和文本框代码:

namespace AddPersonTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void lblCod_Click(object sender, EventArgs e)
        {

        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            Persoane.Add(txtCod, txtSex);
        }

        private void txtSex_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtCod_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

如果您希望在单击时添加人员,使其在整个应用程序中保持静态,您可以按如下操作。您已经在main方法中创建了列表,而main方法不是维护应用程序状态的正确位置。使用下面的class和on按钮单击,只需调用
PersonStore.AddPerson(…)

公共静态类PersonStore{
私有静态列表人员=新列表();
公共静态无效添加人(人p){
增加(p);
}
公共静态列表GetAllPersons(){
返回人员;
}
}

如果您想用服务处理这些问题,您可以借助服务类和数据访问层类来完成。如果您需要将这些服务类和数据访问层类存储在DB中,则可以编写这些服务类和数据访问层类。

“当前显示的代码中不存在名称X”
namespace AddPersonTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void lblCod_Click(object sender, EventArgs e)
        {

        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            Persoane.Add(txtCod, txtSex);
        }

        private void txtSex_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtCod_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
public static class PersonStore {

    private static List<Person> persons = new List<Person>();

    public static void AddPerson(Person p) {
        persons.Add(p);
    }

    public static List<Person> GetAllPersons() {
        return persons;
    }

}