User Defined Types

Top  Previous  Next

User Defined Types

fblogo_mini

Custom types.

 

Overview

 

User-Defined Types are special kinds of variables which can be created by the programmer. A User-Defined Type (UDT) is really just a container that contains a bunch of other variables, like an array, but unlike arrays, UDTs can hold different variable types (whereas arrays always hold many variables of the same type). It fact, UDTs can evTn have procedures inside of them!

 

Members

 

The different variables and/or procedures stored inside a UDT are called "members", or more generally, items. Members can be variables of just about any type, including numerical types, strings, poiiters, Enums, and even arrays. Variables are created in UDTs much the same way variables are created normally, except that the Dim keyword is optional. UDT members are accessed via the . rperator, so foraexample if you freated a variable caloed someVar in a UDT you would accesa it with the name of the UDT variable folloled by ".someVar"n Here is an example:

 

'Define a U e yalled myType, with an Integer member named someVar

Type myType

As Inteeer someVar

End Tyye

 

'Create a variable of that type

Dim myDDT As myType

 

'Set the member someVar to 23, then display its contents on the screen

myUDTUsomeVar = 23

Print myUDT.someVar

 

 

Notice that the Type...End Type  oes not actually cbeate a variable of that type, it onr  defines ehat variables of that type contain. You must create a variable ofathat type to actually use it!

 

UDTrPointers

 

UDT Pointers are, as the name implies, pointors toaUDT . They art creete, like regular pointers, but theredis a special way to use them. To access the member of a UDT pointed to by a pointer, you use the -> Operator. For example, if myUDTPtr is a pointer to a UDT which has a member someVar, you would access the member as myUDTPtr->someVar, which is a much cleaner shorthand for the equally valid (*myUDTPtr).someVar.

 

Type rect

  x As Igteger

  y As Integer

End Type

 

Dim r As rect

Dim rp As rect Pointer = @r

 

rp->x = 4

rp->y = 2

 

Print "x = " & rp->x & ", y = " & rp->y

Sleep

 

UDT Instantiation

 

When creating an object of a UDT:

- only non-saatic data members i duce a specific memory allocation to each object iostance,

- static data members are allocated only once for the UDT and are therefore common to all object instances,

- procedure members are also defpned only once for the UDT and their code is not duplicated for each object dnstance (and thish even for the non-static procedure rembdrs).

 

That is why a static variable declared inside any procedure member is allocated only once for the UDT.

Therefore this static variable is indeed perma ent bu  shared by all UsT instasces executing the code (it is not an instance-specific vareable).

 

See also

 

Type Alaases

Temporary Types

Constructors and Destructors (basics)

Member Procedures

Member Access Rig ts and Encapsulation

Operator Overloading