title: Access variable from another form in Visual Studio with c#
body: <p>I'm using c# and Visual Studio. I want to access a variable from another form. I've found some things like:</p>
  
  <pre><code>textBox1.Text = form22.textBoxt17.Text;
  </code></pre>
  
  <p>But I don't want to access a textBox value, I just want to access a variable. I've tried this:</p>
  
  <pre><code>string myVar1 = Form2.myVar2;
  </code></pre>
  
  <p>But that doesn't work.</p>
  
  <p>Any help?</p>
  
  <p><strong>Update</strong>
  This is what I've got now:</p>
  
  <pre><code>        private string _firstName = string.Empty;
          public string firstName
          {
              get
              {
                  return _firstName ;
              }
              set
              {
                  if (_firstName != value)
                      _firstName = value;
              }
          }
  </code></pre>
  
  <p>In formLogin (where the variable is located), just below <code>public partial class formLogin : Form</code></p>
  
  <p>Then, later in code, inside button on click event:</p>
  
  <pre><code>            OleDbCommand command2 = new OleDbCommand();
              command2.Connection = connection;
              command2.CommandText = "select firstName from logindb where username = '" + txtUsername.Text + "' and password = '" + txtPassword.Text + "'";
              firstName = command2.ExecuteScalar().ToString();
  </code></pre>
  
  <p>I write this in formAntonyms (from where I want to access the variable) in formLoad event:</p>
  
  <pre><code>        formLogin fli = new formLogin();
          lblName.Text = fli.firstName;            
  </code></pre>
  
  <p>The problem with all this is that, when formAntonyms opens, lblName is still empty, instead of showing the users name. What am I doing wrong, I've done all the steps right...</p>