UDT Functions

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > Data types and variables > TYPE (or UDT User Defined Types) > UDT (User Defined Types) > UDT and Functions >

UDT Functions

 

UDT function

 

You can enhance the UDT with functions as well - this approach is appropriate anytime you need to perform the data manipulation of the UDT itself.

 

Imagine our Point2D example - you will most probably need to initialize x and y straight away, and you will most probably need to be able to get string representation of the variable.

 

Let's use this example to demonstrate the use of UDT functions.

 

The first fact you should now is that UDT functions do not take the UDT variable as input explicitly.

 

The data is always reachable via pre-defined ME variable, accessible in each UDT function.

 

There are two kinds of UDT functions:

1.user defined functions

2.functions with special meaning, currently _create and _destroy

 

User defined function within TYPE definition

 

You can define a function directly inside the type / end type block.

 

The same rules apply as for any other function, except the advantage of having ME as a way to reference UDT elements.

 

TYPE Point2D
  x AS SINGLE
  y AS SINGLE
  
  FUNCTION setXY(x AS SINGLE, y AS SINGLE)
    ME.x = x
    ME.y = y
  END FUNCTION
  
  FUNCTION toString() AS STRING
    RETURN strFormat$("[{1}, {2}]"ME.x, ME.y)
  END FUNCTION
END TYPE
 
DIM point AS Point2D
 
point.setXY(1, 2)
msgBox point.toString()

 

The main advantage of this type of definition is that the type / end type contains everything the UDT can do.

 

User defined function outside TYPE definition

 

You can define the function body outside the type / end type block as well.

 

In such a case, please register the function via <functionName> AS FUNCTION within the type / end type first. This registration does not increase the memory footprint of the UDT, but it creates logical link between UDT and function.

 

UDT function defined outside the type / end type block follows the same rules as any other function, except:

1.having ME as a way to reference UDT elements

2.need to prefix the function name with name of type and dot

 

TYPE Point2D
  x AS SINGLE
  y AS SINGLE
 
  setXY    AS FUNCTION
  toString AS FUNCTION  
END TYPE
 
FUNCTION Point2D.setXY(x AS SINGLE, y AS SINGLE)
  ME.x = x
  ME.y = y
END FUNCTION
 
FUNCTION Point2D.toString() AS STRING
  RETURN strFormat$("[{1}, {2}]"ME.x, ME.y)
END FUNCTION
 
DIM point AS Point2D
 
point.setXY(1, 2)
msgBox point.toString()

 

The main advantage of this type of definition is that you can spread the function across multiple source code files.