C# 从另一个窗体访问变量

C# 从另一个窗体访问变量,c#,C#,我有两个表单,一个用于登录,另一个用于调用依赖于登录的函数 表格1 如果我想以另一种形式访问名为student的对象,该怎么办? 我该怎么办?您可以在此表单中声明Student类型的公共属性,并使用Form1.Propertyname 阅读本节中有关属性的更多信息 或者只需将您的对象声明为Public搜索这个完全相同的问题的一百万个副本中的任何一个。这是一个我甚至经常问自己的问题。去检查另一个答案,但为了方便起见,现在有两个选择。首先,将对象公开,通过Form1.objectName访问它。或者

我有两个表单,一个用于登录,另一个用于调用依赖于登录的函数

表格1

如果我想以另一种形式访问名为student的对象,该怎么办?
我该怎么办?

您可以在此表单中声明Student类型的
公共属性
,并使用
Form1.Propertyname

阅读本节中有关属性的更多信息


或者只需将您的对象声明为
Public

搜索这个完全相同的问题的一百万个副本中的任何一个。这是一个我甚至经常问自己的问题。去检查另一个答案,但为了方便起见,现在有两个选择。首先,将对象公开,通过Form1.objectName访问它。或者你的第二个选择是将对象作为参数传递给你的第二个表单。只是出于好奇,这是一个类赋值吗?几个小时前也提出了同样的问题。我真的认为你应该从头开始。。。这就是学习OOP的基础知识,因为我不喜欢你的代码。网络上有很多资源,例如:上面的静态建议只应该在您实际需要静态属性时使用。否则,请引用实际实例。
 public partial class Form1 : Form
    {
        static public string userId, userPassword;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (Program.AllStudents.ContainsKey(userId))
            {
                Program.studentdata student = Program.LoginStudent(userId);
                if (student.password == userPassword)
                {
                    Form3 studentcommands = new Form3();
                    studentcommands.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Wrong username or password", "Error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Wrong username or password", "Error",
     MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }

        }