Structures

Top  Previous  Next

Beginning with V2, Tibbo Basic supports structures. Structure is a combinatorial user-defined data type that includes one or several member variables. Structures are declared using type ... end type statements, as shown in the example below:

 

 

'this is a structure with three members

type my_struct 

       x as byte

       y as Long

       s as string

end type

 

 

In the above example, we declared a structure my_struct that has three member variables: x, y, and s. This is just a declaration -- you still have to define a variable with the new type you have created if you want to use the structure of this type in your program:

 

 

dim var1 as my_struct 'this is how you define a variable with type 'my_struct'

 

 

After that, you can address individual elements of the structure as follows:

 

 

var1.x=5

var1.y=12345678

var1.s="Test"

 

 

Structures you define may include members that are arrays or other structures. Structure variables like var1 above can be arrays as well, of course. In total, Tibbo Basic supports up to eight nesting levels (as in "array within a structure within a structure within and array" --   each structure or array is one level and up to 8 levels are possible). Here is a complex example:

 

 

type foo_struct 'structure with two members, and both are arrays 

  x(10) as byte

  s(10) as string(4)

end type

 

type bar_struct 'structure with two members, one if which is another structure

  foo as foo_struct 'so, this member is a structure

  w as word

end type

 

dim bar(20) as bar_struct 'we define an array of type 'bar_struct'

 

bar(1).foo.s(2)="test" 'oh-ho! we address element 2 of member s of member foo of element 1 of bar!

 

Structures introduce slight overhead

Each structure occupies more space than the sum total of space needed by all of its members. This is because each structure also includes housekeeping data that, for instance, defines how many members are there, what type they have, etc.