The Basics of Programming

Structures

In some computer languages, such as C and C++ you can combine variables into groups called structures or classes. A structure is basically a grouping of values that have a common purpose such as the attributes of window on a computer screen, a character in a game, a customer in a database, a product item, and so on. In Ace Basic, you would use the keyword STRUCT. For Hisoft Basic, you would use TAGLIST. The types of variables can be mixed so you can have BYTE, SHORTINT, LONGINT, ADDRESS, SINGLE and STRING (with SIZE option) variables in the same structure.

Examples,

This ths the structure of an Amiga Window.


STRUCT Window
 SHORTINT left_position
 SHORTINT top_position
 SHORTINT width
 SHORTINT height
 STRING window_title SIZE 20
 BYTE close_gadget
 BYTE minimize_gadget
 BYTE resize_gadget
END STRUCT


This is a stucture of a customer record.
STRUCT customer 
 STRING customer_number SIZE 12
 STRING title SIZE 4
 STRING firstname SIZE 20
 STRING lastname SIZE 20
 STRING street SIZE 20
 STRING city SIZE 20
 STRING county SIZE 20
 STRING country SIZE 20
 STRING postcode SIZE 10
 STRING telephone_number SIZE 20
 SINGLE balance
END STRUCT

To use an instance of a structure, you specify the word DECLARE STRUCT, the name of structure and the name of the variable e.g.
DECLARE STRUCT customer customer1

To use the individual values of a structure you would use the format variable->fieldname, where a dash and right arrow seperates the variable and field name for example, customer1.street.
e.g.
customer1->customer_number = "100101"
customer1->title = "Mr"
customer1->firstname = "Simon"
etc.

Next Page