Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Variables

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • Variables
    Variables are simply names used in a program, where the names represent stored numeric or string values. When an expression contains a variable, such as the expression (x+2), PowerBASIC replaces the variable with its value and computes the value of the expression, as demonstrated in the following example.

        x = 2        'x is a variable that is given the value of 2
        y = x + 2    'y is a variable whose value is now 2 + 2 = 4
    

    AS the example shows, variable values can be changed at any time during the program.

    Variable Declaration
    Declaration of a variable before using it is not required, although a compiler directive #DIM ALL can be added to a program's source code to have the compiler require variable declaration prior to use. It is considered good programming practice to require variable declaration.

    Here's an example of a simple variable declaration, using the DIM statement.

        #DIM ALL          'forces use of DIM to define variables before use
        DIM x as Single   'variable x defined as single precision number
        DIM j As Long     'variable j defines as long integer
        x = 2.3           'without DIM statements, this line causes an error
        j = 5             'without DIM statements, this line causes an error
    

    The other declaration statements, REDIM, LOCAL, GLOBAL, STATIC, and THREADED, are discussed in the section on variable scope.

    Variable Names
    In PowerBASIC all variable names begin with a letter. Variable names can be up to 255 letter and digits,

    Note that long variable names do not affect memory storage requirements. So feel free to use descriptive names of any length to make it easy for you to read and maintain your code. Other that how much typing is involved, there is no penalty for using long variable names.

    Case Sensitivity
    PowerBASIC is not case-sensitive. The variables dog$ and DOG$ are the same variables.

    However, note that x$, x%, and x& are different variables and can coexist in a program. This is not recommended because of the visual confusion it can create.

    Creating a Variable
    Unlike some languages which require that you define a variable before using it, PowerBASIC allows you to define a variable by simply using it in a line of code.

         a$ = 5     #
         b = 2.5    #
    

    When a variable has not yet been assigned a value, PowerBASIC sets the value to undef, which is similar to NULL in other languages. The PowerBASIC defined function can test to see if a variable has been assigned a value.

    Good Variable Naming Practice
    Using variable names which describe the content of the variable is considered good practice. For example, size$ is preferred over $s.

    Programmers also use the following two common variable naming practices, both of which involve using more than one word in the variable name.

         last_index     # words separated by an underscore
         LastIndex      # capitalizing words in the variable name
    

    It is also considered good practice to use Type Specifier suffixes on variable names because it gives a visual indication of the type of data that the variable expects. This is particularly useful during program debugging. Here are the two examples from above, with integer type specifiers included.

         last_index%     # words separated by an underscore
         LastIndex%      # capitalizing words in the variable name
    

    Variable Naming Exceptions - Special Characters
    There are two exceptions to the naming rules - type specifiers and contstants. Type specifiers allow other characters as suffixes to a variable name, whereas constants use variable names with other characters as prefixes to the variable names.

    • Type Specifiers - Suffixes
      As you will see in the section on data types PowerBASIC allows you to define 21 different data types, such as integer, string, long, etc. Most of these data types allow the use of special characters called type specifiers to define the data type of a variable. Type specifiers are suffixes to variable names.

             DIM x As Long
             DIM x%          'type specifier % used to declare x as integer
             DIM x$          'type specifier $ used to declare x as integer
         

    • Constants (Equates) - Prefixes
      PowerBASIC also support constants - named values whose value cannot be changed once the program starts. During compilation, the constant values are replaced with their numeric/string values and minimizes code execution when a program runs.

      PowerBASIC allows both numeric and string equates. The name of a numeric equate always begins with a % character. The name of a string equate always begins with a $ characters. The following examples show how equates are defined.

             %MyEquate = 5          'MyEquate defined as numeric
             $MyEquate = "hello"    'MyEquate defines as string
         

      Equates has global scope and must be defined outside a procedure (Sub/Function), normally at the start of an application's source code. Equates can be defined using other equates, provided the other equates have already been defined. Also, equates must be defined using only simple expressions - those consisting of numeric equates, bitwise operators (such as AND, OR, NOT), arithmetic operators +, -, *, /, and \, and the relational operators >, <, >=, <=, <>, =, and the CVQ function.

      Unlike variables, which are given default values when declared, equates must be assigned before it is referenced, even if that value is zero. Failure to set the value of an equate before use will cause an error during compilation.

    If you have suggestions or corrections, please let me know.