Data types are required if you want to store data - as character-bound information - in a variable. Describe data types:
There are native data types in Gambas. These include, for example, data types with the names (keyword), integer, byte, string or float. An overview of the native data types can be found in table 7.0.1 The syntax of a structure has been introduced in Gambas 3. These structures allow you to combine logically relevant IT units (both data types and classes or objects) into a new unit - a structure. However, this structure has no methods or events. Of particular importance in Gambas as an object-oriented language are the classes. Anything that is not a data type, structure, or executable code is a class. They are characterised by the fact that they are a logical encapsulation of properties, methods and events. An instance of a class is called an object.
Summary
Thus, structures take a mediatingposition between the native data types and the abstract classes (concrete and abstract in the sense of their definition).
Data Type | Description | Default | Memory Size |
---|---|---|---|
Boolean | TRUE or FALSE | FALSE | 1 Byte |
Byte | 0… 255 | 0 | 1 Byte |
Short | -32.768… +32.767 | 0 | 2 Bytes |
Integer | -2.147.483.648… +2.147.483.647 | 0 | 4 Bytes |
Long | -9,223,372,036,854,775,808…. 9,223,372,036,854,775,807 | 0 | 8 bytes |
Single | Like the float-data type in C | 0.0 | 4 Bytes |
Float | Like the double data type in C | 0.0 | 8 bytes |
Date | Date and time, each stored in an integer value | ZERO | 8 bytes |
String | A variable-length string with variable length | ZERO | 4 bytes |
Variant | Every data type | ZERO | 12 bytes |
Object | Anonymous reference to an object | ZERO | 4 bytes |
Pointer | Pointer to a memory address | 0 | 4 bytes on 32-bit systems and 8 bytes on 64-bit systems |
Table 7.0.1: Overview of the native data types in Gambas
The native data types have an associated array data type whose name corresponds to the name of the native data type and follows the square brackets: {code_b_0}
The description of arrays can be found in chapter 20.12.
You can use the following functions to determine the data type. The argument passed to the data type functions is a string or a data type in SizeOf () or an expression in TypeOf (). The function value is either True or False or a memory value or a data type.
Function | Description |
---|---|
IsBoolean (string) | Is true if string=“True” or string=“False” (comparison is not case-sensitive) |
IsDate (string) | Is true if string can be safely interpreted as date |
IsFloat (string) | Is true if string can be safely interpreted as float number |
IsInteger (string) | Is true if string can be safely interpreted as an integer |
IsLong (string) | Is true if string can be safely interpreted as a long integer number |
IsNumber (string) | Is true if string can be safely interpreted as a number |
IsZero (string) | Is true, if string is safe ZERO ist |
Table 7.0.2.2: Overview of the data type functions 1
Function | Description |
---|---|
SizeOf (data type) | Specifies the memory used by a specific data type. |
TypeOf (expression) | Returns the data type of an expression. |
Table 7.0.2.3: Overview of Data Type Functions 2
The function value of SizeOf (data type) or TypeOf (expression) is one of the following constants:
Expression type | Return value (numeric) | Return value (symbolic) |
---|---|---|
Byte | 2 | gb.Byte |
Short | 3 | gb.Short |
Integer | 4 | gb.Integer |
Long | 5 | gb.Long |
Single | 6 | gb.Single |
Float | 7 | gb.Float |
Date | 8 | gb.Date |
String | 9 | gb.String |
Pointer | 11 | gb.Pointer |
Variant | 12 | gb.Variant |
Function | 13 | gb.Function |
Class | 14 | gb.Class |
Object | 16 | gb.Object |
Table 7.0.2.3: Overview of the function values of SizeOf (data type) and TypeOf (expression)
Hints:
Gambas provides functions for converting data types. For information on these functions, see chapter 9.7 Conversion Functions.
However, Gambas also automatically converts data types. For example,“2” is a string, but can also be interpreted as an integer 2 (integer) and can be used “on the fly” after a conversion, as the following examples show:
Public Sub OnTheFly_Click() Print "2" + 2 ' Integer-Number Print "Can '2' be interpreted as an integer number? ---> ";; IsInteger("2") Print "Type_1 = ";; TypeOf("2" + 2) ' 7 => Float ' String + Integer ' String -> Integer => 2 + 2 = 4 Print "2" & 2 ' String Print "Type_2 = ";; TypeOf("2" & 2) ' 9 => String ' String & Integer ' Integer -> String => "2" & "2" = "22" Print 2.1 * ("2" & 2) ' Floating-point number Print "Type_3 = ";; TypeOf(2.1 * ("2" & 2)) ' 7 => Float ' Float * (String & Integer) ' Integer -> String => "2" & "2" = "22" ' Float * String => 2.1 * "22" ' String -> Float => 2.1 * 22.0 = 46.2 End ' OnTheFly()
These are the expenses in the console:
4 Can '2' be interpreted as an integer number? ---> True Type_1 = 7 22 Type_2 = 9 46,2 Type_3 = 7