Slide 8.8: Variables
Slide 8.10: Operators
Home

Variables (Cont.)


Working with Variables
The general format of an assignment is
   Variable = Expression;
The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a Boolean value (true or false), etc. The following are some examples:
   userpass.Text    = password;
   command1.Visible = false;
   thirdNumber      = int.Parse( usernum1.Text );
It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result. The following example uses various types of variables:

Demonstration of Various Types of Variables
using System;
namespace VariableDefinition {
  class Program {
    static void Main( string[ ] args ) {
      // Declaration
      short  a;
      int    b;
      double c;
      // Actual initialization
      a = 10;
      b = 20;
      c = a + b;
      // Output     
      Console.WriteLine( "a = {0}, b = {1}, c = {2}", a, b, c );
    }
  }
}