<< Prev        Next >>

ID : 5075

Variable Type and Precautions for Use

Classification of Variables

  • There are two types of variables, global variables and local variables.
  • Local variables include Static variables (local variables with Static attribute).
  • Static variables include public variables (local variables with Public attribute).
Variable types Local Variable   Global variable
Static Attribute  
Public Attribute
Created by user? Yes Yes Yes No
Cleared when the controller turns off - Not cleared Not cleared Not cleared
Accessible from other program files? - - Accessible Accessible

Kind and Type of Variables.

  Name of variable Local variable Static variable Public variable Global variable
Variable type Integer X X

X

X I
Single F
Double D
Vector V
Position P
Joint J
Trans T
String X*1 X*1 X*1 S
Variant        
Object        

X*1:Maximum data size is 255 byte.

Creation, Initialization and Deletion of Static Variables.

This section describes the information about creation, initialization and deletion of static variables (include public variables).

Creation of Variables

Static variable will be created (area assigned) when a program file which is added variables is compiled at a first time.

Initialization of Variables

In the following cases, all the static variables included in the program are initialized when compiling the program file.

  1. When one or more static variables are added, deleted or changed.
    In this case, the word "change" means one or more changes of variable name, type, dimension and initial value.
    This also includes a static variable used in a program file called by "Include" command.
  2. When a name of program file or a folder which defines a variable is changed.

Precautions for Initialization

When a variable of a program file is initialized, it may causes an error of another program file which is referring to the variable.
If a variable of a program file is initialized, other program files referring to the variable may cause some errors. To avoid that, use one of following way.

  1. Forcefully initialize program files which are referring to the initialized variable.
    A change of static variable triggers initialization. Utilizing this property, change one of the static variable value temporarily, then return the value to its original state.
  2. In the program file which is referring the variable, create a program to detect the inconsistency and to rewrite the value.

Note that errors and inconsistency may occur when sending WINCAPSIII programs or reading a program stored in USB also.

Deletion of Variable

If a variable is deleted from a program file, the variable is deleted at the timing of compiling the program file.
If a program file is deleted, the variable is deleted at the timing of compiling other programs.

Example of Static Variable

Static variables (include public variables) retain data even after turning the power off. This property makes sure the independency of each data.
By utilizing this property, functions explained in following examples are realized.

Example 1 : Output a number of program execution

RunCount command counts up for every execution of the following program.

Pro1.pcs

Sub Main 
 Static RunCount as Integer = 0
 PrintDbg "The latest executed times = " & RunCount

  ' ... Procedures to be executed

 RunCount = RunCount+1
End Sub

Example 2 :Use user original data area

In the program below, assign 100 of data area in DataBase.pcs then set values by Pro1.pcs.

DataBase.pcs

#define DB_MAX  100

Public Count As Integer = 0 'Counter 
Public ProductID(DB_MAX) As Integer 'Product ID 
Public ProductName(DB_MAX) As String 'Product Name
Public ProductPos(DB_MAX) As Position 'Position

Sub Main 

End Sub

Pro1.pcs

Sub Main 
  Dim n as integer
  GetPublicValue n, "DataBase", "Count"

  ' 0, "N000", P(100, 120, 400)
  SetPublicValue 0, "DataBase", "ProductID", n 
  SetPublicValue "N000", "DataBase", "ProductName", n 
  SetPublicValue P(100, 120, 400), "DataBase", "ProductPos", n 

  ' 1, "N001", P(200, 220, 300)
  SetPublicValue 1, "DataBase", "ProductID", n 
  SetPublicValue "N001", "DataBase", "ProductName", n 
  SetPublicValue P(200, 220, 300), "DataBase", "ProductPos", n 

  SetPublicValue n+1, "DataBase", "Count" 
End Sub

ID : 5075

<< Prev        Next >>