Defining and using NEWTYPEs
NEWTYPEs are exactly that - a way of defining new types in addition to the six basic types (byte, word, long, quick, float and string) already available in Blitz. Newtypes are structures built out of the basic types and other already defined newtypes.
Newtypes are usually used to create a type which allows you to store different but related information in a single variable. A typical use for this would be a record in a database - you need to store different information (name, address, books borrowed, etc.) that is related (all that information would be for one person). The easiest way to do this would be to create a newtype to allow all this information to be stored in one place (i.e. in one variable).
The first thing to do to be able to use a newtype is to create one. To start the definition of a newtype, you use 'NEWTYPE.<new_type_name>'. The type name must be unique, although for good programming practice you should give it a meaningful name. To end a definition, you use 'End NEWTYPE'.
To define the contents of the type, you must define the variables (and their types) inside the NEWTYPE...End NEWTYPE lines. These variable definitions must be in the form 'variable_name.type' and not using the DEFTYPE command. These variables within the newtype are often called the fields of the newtype. You can even use newtypes as the type of a field, but it must have already been defined before you use it. You can use the colon character to put multiple fields on one line - doing this also has the advantage that you may omit the type of the second, third, fourth, etc fields and they will be given the same type as the first field on that line. The same suggestion about field names apply from the variables topic - keep the names meaningful and consistent.
You will have noticed that inside the newtype definition, each line was indented by a couple of spaces. This is another point about keeping code readable - indentation. It makes the contents of a block of code (in this case the code between the NEWTYPE...End NEWTYPE) more readable, and you can see that it is all related. The amount you indent by does not really matter, as long as you keep it consistent.
You can even put arrays inside newtype definitions, although this will be discussed in later pages of this topic. Now that you know how to define a type, you need to be able to use it. Defining a variable with your newtype is exactly the same as defining a variable with one of the basic types, except you substitute your type name instead of the basic type name. To recap, you can either use DEFTYPE or simply use the variable for the first time. However, if you are using the variable for the first time, you will probably need to access one of the fields or it will not make much sense. To access the fields in a newtype, you use the format 'variable_name\field_name' and you never include the type on the end of a field, not even when you defined a string using the dollar sign at the end:
So that's how to use newtypes with variables. The main points to remember are (in no particular order):
Installation | Troubleshooting | Programming | Archives | Links
Page last modified: 14th September 2001