I'm new to c# programming, and I am a little stuck on how to make a variable available across multiple forms. I read that i should make a public static class, however, my code is as follows:

private void btnLogin_Click(object sender, EventArgs e)
        {
            accountNo = txtBoxAccntNo.Text;
            valid = validate();  //uses validate() method to check validity 
            if (valid == true && accountNo == "11111111")
            {
                ManagerScreen Manager = new ManagerScreen();
                this.Hide();
                Manager.Show();
            }
            else if (valid == true)
            {
                s customer = new s();
                this.Hide();
                customer.Show();
            }
            else
            {
                if (count == 2)
                {
                    this.Close();
                }
                count += 1;
                txtBoxAccntNo.Clear();
                txtBoxPinNo.Clear();
            }

        } 

I want to make the variable AccountNo available on other forms, however, when its like this i'm just not sure how to. I'm sure there is a simple solution and any advice is appreciated.

Sincerely, 
        p.w

Check this thread.

static class GlobalClass
 {
 private [U]static[/U] string m_globalVar = "";
 public static string GlobalVar
{
   get { return m_globalVar; }
  set { m_globalVar = value; }
}
}

It states Add a new class.cs file to the project and add static class and static variable. Then to call it use classname.variable.
Like in our case it would be GlobalClass.m_globalVar.

Well, with the property it would be GlobalClass.GlobalVar, you don't have access to m_globalVar.

I'm sorry but I still don't understand..

Something that is declared static belongs to a class, not to an instance of the class. So we have

public MyClass {
    public static int MyInt;   // static, so it belongs to the class, there is only one
    public double MyDouble;   // Not static, is only available to instances of the class
}

public MyMainClass {
    static void Main() {
        MyClass.MyInt = 4; // accessed through the class

        MyClass myInstance = new MyClass();  // delcared an instance of the class

        myInstance.MyDouble = 2.3;   // We have to use an instance to get to this, and
                                     // it would be different for a different instance.

        myInstance.MyInt = 5;   // Won't compile, can't use an instance to get a class variable

        MyClass.MyDouble = 32.2; // Won't compile, can't use the class to get to instance variable
    }
}

MyInt is now a 'global' and can be accessed anywhere in your program by referencing it through the class (again MyClass.MyInt).

I'm still struggling, can anyone recommend some good tutorials on classes in visual c#?

What about this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.