C# 从两个窗体的公共类获取变量

C# 从两个窗体的公共类获取变量,c#,.net,C#,.net,我想从现有表单中打开一个新表单-AdminForm。新表单要求输入密码,该密码应传输到main表单中的变量 为此,我创建了一个类: public class Decrypt { public static string AdminPass { get; set; } } 我在AdminForm中引用了public类,并在AdminPass变量中设置了一个值 public Decrypt AdminPass { get; set; } private void button1_

我想从现有表单中打开一个新表单-AdminForm。新表单要求输入密码,该密码应传输到main表单中的变量

为此,我创建了一个类:

public class Decrypt
{    
    public static string AdminPass { get; set; }
}
我在AdminForm中引用了public类,并在AdminPass变量中设置了一个值

public Decrypt AdminPass { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    if (txtAdminPass.Text == "2017")
    {
        Decrypt.AdminPass = "yes";
        this.Close();
    } 
    else 
    {
        MessageBox.Show("Incorrect. Try again.");
    }
}
最后,我尝试访问main表单中的变量,如下所示:

private void btnDecrypt_Click(object sender, EventArgs e)
{
    using (AdminForm openForm = new AdminForm() { AdminPass = new Decrypt() })
    {
        if (openForm.ShowDialog() == DialogResult.OK)
        {    
            label23.Text = Decrypt.AdminPass;
        }

    }
}
编辑:要解密的变量。AdminPass

不过。该变量似乎仅在
AdminForm
中分配。所以如果我执行
MessageBox.Show(Decrypt.AdminPass)
AdminForm
中,打印
字符串“Yes”
。但是在
MainForm
中,
label23.Text
保持不变

我哪里弄错了,有什么线索吗


我知道这是一个基本问题,但我对C#还很陌生

从静态方法调用非静态属性。尝试将属性设置为静态(假设它们在同一个类中)为什么不在获取时使用
Decrypt.AdminPass
,agreed@MikeMcCaughan是我干的。代码已编译,但无法检索字符串值。我已更新了问题。回头看,这看起来很明显。谢谢
if (txtAdminPass.Text == "2017")
{
    Decrypt.AdminPass = "yes";
    //this.Close();
    DialogResult = DialogResult.OK;  // !!!
} 
else ...