SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

PURPOSE:

DECLARE is used to specify the data types of variables, functions, etc. Once the data type of a variable or function has been declared, it is not necessary to designate them with a trailing $ or %.

DESCRIPTION:

DECLARE declares the data type of a variable or function. The STRING option indicates that the following are string variables or functions. INTEGER declares integer numeric. REAL indicates real numeric. The BOOLEAN option indicates that the following are Boolean variables. Only one of the four data type options can be used in each DECLARE statement. Any number of variables can be declared with a DECLARE statement.

DECLARE OBJECT declares one or more variables to be of type OBJECT. A variable of type OBJECT receives the data type of the data that is put into it.

Note

OBJECT and DYNAMIC are synonyms in this statement. DECLARE DYNAMIC is the same as DECLARE OBJECT.

To find out the current data type of an object variable, the DTYPE function can be used (see Section 6.9.2).

5.1.1 Multiple Data Types

Multiple data types can be declared by using the following format:


        DECLARE datatype var, var, datatype var, var, datatype var, var, ... 

For example:

Example 5-2 Declaring multiple data types

  declare string name, sex, integer age, year, real amount 
  input 'Enter your name': name 
  input 'Enter your age': age 
  input 'Enter your sex': sex 
  input 'Enter the year': year 
  input 'Enter an amount': amount 
  print
  print name; ' is a'; age; 'year old '; sex; ' in'; year 
  print name; ' entered the amount'; amount 
  end
 
 
Enter your name? Terry 
Enter your age? 25 
Enter your sex? female 
Enter the year? 2000 
Enter an amount? 582.69 
 
Terry is a 25 year old female in 2000 
Terry entered the amount 582.69 

5.1.2 DECLARE STRUCTURE

FORMAT:


        DECLARE STRUCTURE struc_name1 [, struc_name2 ...] 

EXAMPLE:

Example 5-3 DECLARE STRUCTURE

  declare structure str 
  open structure cl: name 'sptools:client' 
  ask structure cl: id cl_id$ 
  set structure str: id cl_id$ 
  extract structure str 
  end extract
  for each str 
    print str(#1); ' '; str(#2) 
  next str 
  end
 
 
20000 Smith 
20001 Jones 
20002 Kent 
23422 Johnson 
32001 Waters 
43223 Errant 
80542 Brock 
80543 Cass 
80544 Porter 
80561 Derringer 
80573 Farmer 

DESCRIPTION:

DECLARE STRUCTURE declares one or more symbols to be of type STRUCTURE. Once a symbol has been declared to be of type STRUCTURE, it can be used in statements such as SET STRUCTURE...ID to write generalized routines where you do not know at compile time which structure you are going to use.

Usage example: this statement could be used in the situation where you have a transaction structure and a transaction history structure and, optionally, want a report on one or the other. You could use one report program and the DECLARE STRUCTURE statement to declare which structure to use when the user makes the report selection.

5.2 OPTION Statements

5.2.1 OPTION REQUIRE DECLARE

FORMAT:


        OPTION REQUIRE DECLARE 

EXAMPLE:

Example 5-4 OPTION REQUIRE DECLARE statement

  option require declare
  declare string name, comment 
  input 'Please enter your name': name 
  line input 'Enter a comment in quotes': comment 
  print name; ' says, '; comment 
  end
 
 
Please enter your name? George 
Enter a comment in quotes? 'Have a nice day!' 
George says, 'Have a nice day!' 

DESCRIPTION:

OPTION REQUIRE DECLARE causes SheerPower to require all variables in the program to be declared. If the OPTION REQUIRE DECLARE statement is used and a variable is left undeclared, SheerPower will return an error when program execution is attempted. The OPTION REQUIRE DECLARE statement should occur before any DECLARE statements and before any variables are assigned.

5.2.2 OPTION BASE

FORMAT:


        OPTION BASE  [0 | 1] 

DESCRIPTION:

OPTION BASE sets the lowest subscript or base for arrays. The base can be either zero or one. If you use OPTION BASE 0, the lowest element of an array has the subscript zero (0). If you use OPTION BASE 1, the lowest element is subscript one (1).

See Section 5.8.4 for an example and detailed information on this statement.

5.3 LET

FORMAT:


        [LET] var = expr 

EXAMPLE:

Example 5-5 LET statement

  input 'Last name': last$ 
  input 'First name': first$ 
  let name$ = first$ & ' ' & last$ 
  print name$ 
  end
 
 
Last name? Taylor 
First name? Rick 
Rick Taylor 

PURPOSE:

The LET statement is used to store information into a variable or data structure.

DESCRIPTION:

var is the variable being assigned a value. expr is an expression. The expression is evaluated and its result is assigned to the variable. The expression can be any SheerPower expression (see Chapter 4.) The variable and the expression data types must match. For instance, if var is a string variable, expr must be a string expression.

NOTE: The keyword LET is optional. For example:


        LET name$ = first$ & ' ' & last$ 

can be stated as:


        name$ = first$ & ' ' & last$ 

When SheerPower executes the LET statement, it first evaluates the expression on the right side of the equal sign. It then assigns this value to the variable on the left side of the equal sign. The variable represents a location in memory. The value of the expression is stored in this location. Each time a new value is assigned, the old value is lost and the new value is stored in its memory location.

Assigning numeric values:

Example 5-6 Assigning numeric values with LET statement

  input 'Amount': amount 
  let rounded% = amount 
  print 'Real numeric amount:'; amount 
  print 'Integer amount (after rounding):'; rounded% 
  end
 
 
Amount? 1.54 
Real numeric amount: 1.54 
Integer amount (after rounding): 2 

Incrementing Numeric Variables

FORMAT:


        num_var++ 

It is common place to increment a numeric variable:


  total = total + 1 
 
  lines = lines + 1 

SheerPower 4GL provides a shorthand syntax for incrementing numeric variables:


  total++ 
 
  lines++ 

5.4 Assigning string values

5.4.1 LSET, RSET and CSET

LSET, RSET and CSET assign string values to variables. LSET left-justifies, RSET right-justifies, and CSET center-justifies the new value. These statements can be used to assign only string values.

LSET, RSET and CSET justify the new value within the length of the previous value. For example, in the following program, HEADING$ has a length of twenty characters and consists of twenty dots:

Example 5-7 LSET, RSET and CSET

  heading$ = repeat$('.', 20)          // Twenty dots 
  print '('; heading$; ')' 
  end
 
 
(....................) 

In the following example, the RSET statement is used to assign the new value 'Page 12' to HEADING$. SheerPower uses the current length of HEADING$, 20 characters, and replaces it with the new value, 'Page 12'. SheerPower right-justifies this value by padding it with 13 leading spaces. Thus, HEADING$ still has a length of twenty characters.

Example 5-8 RSET statement

  heading$ = repeat$('.', 20)          //  Twenty dots 
  print '('; heading$; ')' 
  rset heading$ = 'Page 12' 
  print '('; heading$; ')' 
  end
 
 
(....................) 
(             Page 12) 


Previous Next Contents Index