IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceLanguage Basics  Print This     

Variables (Cross Ref > Language Basics)

Variables

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

Access VBA:   Dim x as Integer

Access VBA is a loosely typed language. Declaring variables is optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim, Private, Public, or ReDim. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.

Undeclared variables are variants. To specifically declare a variant, use:

Dim x As Variant
Dim x 

Common data types include Byte (0..255), Boolean, Integer (2-byte integers), Long (4-byte integers), Currency, Single (32-bit number), Double (64-bit number), Date, String, and variant.

Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

Syntax Example:
Dim FullName As String
Dim Age As Integer
Dim Weight As Double
 
FullName = "Mike Prestwood"
Age = 32
Weight = 154.4
 
'Declaritive assignment not supported:
''Dim Married As String = "Y"   '>>>Not supported.

Require Variable Declaration

It is a good idea to declare variables. Using only declared variables prevents common mistakes. As the first line of code, add the following to force variable declarations with Dim:

Option Explicit

Add Option Explicit Automatically setting

While editing code, click Tools | Options and check the Require Variable Declaration checkbox to have Access automatically add the Option Explicit directive to all newly created code modules (not retroactive).

More Info

Code:  Access VBA Variables (Dim x as Integer)

ASP Classic:   Dim x

ASP Classic is a loosely typed language. No variable types in ASP (all variables are variants). Declaring variables is even optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim in that script. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.

For example, at the top of my common include file, I have the following:

<%@LANGUAGE=VBScript%>
<%
Option Explicit
'...more code here.
%>
Syntax Example:
Dim Fullname
Dim Age
Dim Weight
 
FullName = "Mike Prestwood"
Age = 32
Weight = 154.4
 
'Declaritive assignment not supported:
''Dim Married = "Y"   '>>>Not supported.

More Info

C#:   Int16 x=0;

C++, Java, and C# all use C-like variable declaration.

C# has C-like variable declaration and although variables are case sensitive, VS.Net will auto-fix your variable names to the defined case.

C# offers many variable types. Some common types used include short, intlong, float, double, decimal, Int16, UInt16, Int32, Int64, string, and bool.

You can also specify the value when you declare a variable as in:

String FirstName = "Mike";
String LastName = "Prestwood";
Int16 Age = 42;
Syntax Example:
string FullName;
int16 Age;
double Weight;
 
FullName = "Mike Prestwood";
Age = 32;
Weight = 154.4;


More Info

C++:   int x=0;

Variable names are case sensitive.

The fundamental variable types in C++ are char, short int, int, long int, bool, float, double, long double, and wchar_t. The integer data types char, short, long and int can be either signed or unsigned. Signed variables are positive and negative. Unsigned variables are positive only.

Syntax Example:

C++, Java, and C# all use C-like variable declaration.

int Age;
int Age = 43;
float a, b, c;
string FullName; //#include 
unsigned short int FamilyMemberCount;

More Info

C++/CLI: 

Corel Paradox:   var x SmallInt endVar

Declaring variables is optional unless you click Program | Compiler Warnings while in the ObjectPAL editor for every form, script, and library you create. Using Compiler Warnings is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope. Also recommended is turning on Compile with Debug for every form, script, and library too for tighter, cleaner code.

Undeclared variables are AnyType variables. Common data types include Currency, Date, Datetime, Logical, LongInt, Number, SmallInt, String, and Time.

Syntax Example:
var
    FullName String
    Age SmallInt
    Weight Number
endVar
 
FullName = "Mike Prestwood"
Age = 32
Weight =154.4
msgInfo("", FullName + ", age=" + String(Age) 
             + ", weight=" + String(Weight))

Declare local variables within a method. If you want a local static variable (retains it's value because it is not destroyed), declare the variables above the method. Variables declared in an object's Var window are visible to all methods attached to that object, and objects that it contains.



Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: True or False?

Declaring variables is optional unless you turn on compiler warnings (for example with the menu option Program | Compiler Warnings).

Answer:
  • True
  • False
  • More Info

    Code:  ObjectPAL Variables (var x SmallInt endVar)

    Delphi:   var x: Integer = 0;

    The Delphi language is a strongly typed language so you have to specifically declare variables and frequently use commands such as IntToStr and StrToInt.

    Declare global variables in the interface section of a unit, variables declared within the implementation section (but not within a method) have a scope limited to the unit. You declare local variables in a var block outside (above) your begin..end code block. You cannot declare variables in-line (inside begin..end).

    You can initialize global and unit variables but you cannot initialize local variables.

    Delphi offers many variable types. Some common variable types include String, WideString, PCharInteger, Boolean, Single, Double, Pointer, and Variant.

    Note: D2009 introduced a new UnicodeString type.

    Syntax Example:
    procedure SomeProcedure;
    var
      Fullname: String;
      Age: Integer;
      X, Y, Z: Double;
      MyArray: array [1..100] of Char;
    begin
    end;
     
    //You can initialize global variables.
    var
      ClickCounter: Integer = 0;

    Interface versus Implementation

    Variables declared in the interface section of a unit are truly global and you should limit the number of variables you declare in the interface section especially for reusable units (units that contain classes). Variables declared in the implementation section of a unit have a scope limited to the unit.

    Initializing Local Variables

    You cannot initialize local variables. The following commented out code, does not work:

    procedure TForm2.Button6Click(Sender: TObject);
    var
    //  ButtonClicks: Integer = 0;  //Does not work!
    begin
    end;

    Initializing Global Variables

    You can initialize global variables but not local variables. Suppose you wish to allow a user to click a button up to 3 times. You can initialize a global variable to track clicks.

    The following code does work:

    var
    ClickCounter: Integer = 0; //Does work!
    procedure TForm2.Button7Click(Sender: TObject);
    begin
    If ButtonClicks >= 3 then
    ShowMessage('Stop clicking the button.')
    Else
    begin
    ButtonClicks := ButtonClicks + 1;
    Form2.Caption := IntToStr(ButtonClicks);
    end;
    end;

    Instance Counter

    Initialized Global Variables and Static Data: Initialized global variables are important for many reasons but I will discuss it's relation to static class data here. Static class data is data of a class that retains state (it's value) whether or not there is an instance of a class. Suppose you wish to have an instance counter. If no classes are currently created, the current value of your instance counter needs to be 0. For each class created, you add 1. For each destroyed, you subtract 1.



    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    The correct syntax for a declaritive variable assignment is?

    Answer:
    1. 
    Dim Married As String = "N"
    2. 
    Married : String = 'N';
    3. 
    Married String := 'N';
    4. 
    Married : String := 'N';
    5. 
    String Married = "N";

    More Info

    KB Post:  Delphi Instance Counter
    Code:  Delphi Variables (var x: Integer = 0;)

    Delphi Prism:   var x: Integer := 0;

    Prism supports type inference where you just use a variable and the compiler will then choose the lowest type possible (such as an Integer before a LongInt). With Prism, you frequently do not have to use commands to convert from one type to another.

    Variable names are not case sensitive. The Prism language offers both old-style declaring variables before the begin as well as in-line variable declaration.

    Prism does support variable initialization too.

    Prism offers many variable types. Some common variable types include Integer, LongInt, Single, Double, Boolean, and String.

    Syntax Example:
    var
    FName: String; //This is old-style.
    begin
    FName := "Mike Prestwood";
    MessageBox.Show(FName);

    Var Age: LongInt; //Local variables.
    Age:=36;
    MessageBox.Show(Fname + " is " + Age + " years old");

    //Assign values too...
    Var Wife: String:="Lisa"; Var WifeAge: Integer:=32;
    messagebox.Show(wife + " is " + Wifeage + ".");
    end;




    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    The correct syntax for a declaritive variable assignment is?

    Answer:
    1. 
    Married : String = 'N';
    2. 
    Dim Married As String = "N"
    3. 
    Married String := "N";
    4. 
    Married : String := "N";
    5. 
    String Married = "N";

    More Info

     

    Java:   int x = 0;

    Variable names are case sensitive.

    The Java basic types are boolean, byte, short, int, long, float, double, and char. You can also declare a variable to hold a particular instance of a class such as String.

    Syntax Example:

    C++, Java, and C# all use C-like variable declaration.

    int a;
    int a, b;
    int age = 43;
    String FullName;

    More Info

    JavaScript:   var x = 0;

    JavaScript is a loosely typed language. Each variable is cast in usage as string, number, boolean, function, or object.

    Variable names are case sensitive.

    Alternatively, you can specify the value when you declare a variable:

    var FirstName = "Mike";
    var LastName = "Prestwood";
    var Age = 42;
    Syntax Example:
    var FirstName;
    var LastName;
    var Age;

    More Info

    Perl:   $x = 0;

    Perl is a loosely typed language with only three types of variables: scalars, arrays, and hashes. Use $ for a scalar variable, @ for an array, or % for a hash (an associative array).

    The scalar variable type is used for any type of simple data such as strings, integers, and numbers. In Perl, you identify and use a variable with a $ even within strings.

    Syntax Example:
    #!/usr/local/bin/perl -w
     
    print("Content-type: text/html\n\n");

    $fullname = 'Mike Prestwood';
    $Age = 38;
    $Weight = 162.4;
     

    print "Your name is $fullname.
    ";
    print "You are $Age and weigh $Weight.
    ";

    Note: In PHP, you declare constants similar to how you  declare variables except you drop  the $.

    Complete Example

    Here is a complete example that demonstrates a few concepts (refer to comments in code):

    #!/usr/local/bin/perl -w
    print("Content-type: text/html\n\n");
    print("");
    print("");
    print("");

    #
    #Variable are case sensitive.
    #
    $fullname = 'Mike Prestwood';
    $FullName = 'Wes Peterson';

    print "Perl vars are case sensitive: " + $FullName + "
    ";

    $Age = 38;
    $Weight = 162.4;

    print "Your name is $fullname.
    ";
    print "You are $Age and weigh $Weight.
    ";

    #
    #Now using quotes.
    #
    $fname = "Mike";
    $lname = "Prestwood";

    $fullname = $fname . $lname;

    print $fullname . '
    ';

    #
    #Two literals too:
    #
    print "My name is " . "Mike.
    ";

    #
    #Long strings.
    #
    $MyMsg = "This is a long string and
    unlike some other languages. PHP allows
    you to put strings on multiple lines 
    like this.";

    print $MyMsg;

    print("");

    More Info

    PHP:   $x = 0;

    PHP is a loosely typed language. No variable types in PHP. Declaring and using variables are a bit different than in other languages. In PHP, you identify and use a variable with a $ even within strings!

    You assign by reference with & as in &$MyVar.

    Syntax Example:
    $fullname = 'Mike Prestwood';
    $FullName = 'Wes Peterson'; //This is a different variable!
    $Age = 38;
    $Weight = 162.4;
     

    echo "Your name is $fullname.
    ";
    echo "You are $Age and weigh $Weight.
    ";

    Some PHP Examples

    Just like HTML, quotes are single or double:

    <?PHP
    echo "Mike's drums are over there.<br>";
    echo 'Mike said, "hi!"<br>';
    ?>

    You don't specify the variable type, the interpreter will automatically use a variant-type variable. To Declare, just assign:

    <?PHP
    $fullname = 'Mike Prestwood';
    $FullName = 'Wes Peterson';
    $Age = 38;
    $Weight = 162.4;

    //Variable within a literal.
    echo "Your name is $fullname.<br>";
    echo "You are $Age and weigh $Weight.<br>";

    //$ within a literal ok too.
    echo "That will be $1.52.<br>";
    ?>

     

    PHP is case sensitive with variables too:

    <?PHP
    $fullname = 'Mike Prestwood'; //This is different...
    $FullName = 'Wes Peterson';   //than this.

    echo $fullname;
    echo $FullName;
    ?>

    By Reference uses "&" as in:

    <?PHP
    $MyOriginalVar = "Mike";
    $MyNewVar = &$fullname;

    echo $MyOriginalVar; //Mike
    echo $MyNewVar; //Mike

    $MyNewVar = "Lisa";
    echo $MyOriginalVar; //Lisa (original changed!)
    echo $MyNewVar; //Lisa
    ?>

    More Info

    Code:  PHP Variables ($x = 0;)

    VB Classic:   Dim x As Integer

    VB Classic is a loosely typed language. Declaring variables is optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim, Private, Public, or ReDim. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.

    Undeclared variables are variants. To specifically declare a variant, use:

    Dim x As Variant
    Dim x 

    Common data types include Byte (0..255), Boolean, Integer (2-byte integers), Long (4-byte integers), Currency, Single (32-bit number), Double (64-bit number), Date, String, and variant.

    Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

    Syntax Example:
    Dim FullName As String
    Dim Age As Integer
    Dim Weight As Double
      
    FullName = "Mike Prestwood"
    Age = 32
    Weight = 154.4
     
    'Declaritive assignment not supported:
    ''Dim Married As String = "Y"   '>>>Not supported.

    Require Variable Declaration

    It is a good idea to de clare variables. Using only de clared variables prevents common mistakes. As the first line of code in the module, add the following to force variable declarations with Dim:

    Option Explicit

    Add Option Explicit Automatically setting

    While editing code, click Tools | Options and check the Require Variable Declaration checkbox to have Access automatically add the Option Explicit directive to all newly created code modules (not retroactive).

    More Info

    Code:  VB Classic Variables (Dim x As Integer)

    VB.Net:   Dim x As Integer=0

    Variables are case sensitive but VS.Net will auto-fix your variable names to the defined case. You can declare variables in-line wherever you need them and declarative variable assignment is supported.

    Syntax Example:

    Dim FullName As String

    Dim Age As Integer

    Dim Weight As Double

    FullName = "Mike Prestwood"

    Age = 32

    Weight = 154.4

    'Declaritive variable assignment:

    Dim Married As String = "Y"

    MsgBox(Married)




    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    The correct syntax for a declaritive variable assignment is?

    Answer:
    1. 
    Dim Married As String := "N"
    2. 
    Dim Married As String = "N"
    3. 
    Dim Married As String == "N"
    4. 
    Dim Married As String, Married = "N"
    5. 

    None of the above.

    More Info

     




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2025 Prestwood IT Solutions.   [Security & Privacy]