从另一种形式访问变量

时间:2016-12-14 20:34:12

标签: c#

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

FORM1

 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();
            }

        }

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

1 个答案:

答案 0 :(得分:2)

您可以在此表单中声明学生类型的Public property,并使用Form1.Propertyname

从其他人访问它

详细了解此MSDN topic

中的属性

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

相关问题