NEW

Previous  Next

 

reference = NEW( type     AS TYPE, 

                [dim1     AS INTEGER=0],

                [dim2     AS INTEGER=0],

                [dim3     AS INTEGER=0],

                [dim4     AS INTEGER=0],

                [name     AS STRING=""],

                [global1  AS INTEGER=FALSE] ) AS REFERENCE

 

reference = NEW( AS, 

                 array    AS ARRAY|REFERENCE,

                [name     AS STRING=""],

                [global1  AS INTEGER=FALSE] ) AS REFERENCE

 


 

Allocates a new variable or array.  If name is omitted, MyBASIC2 will assign it a unique temp name.  The function returns a reference to the new variable.  The variable will be deleted when using UNREF.  If a reference is already assigned, the function will throw an error.

 

The dimensions parameters, dim1 through dim4,  follows the same convention as the DIM statement when declaring an array.

 

The global parameter determines if the allocated variable will be allocated at the local or global scope.  To make sure that the variable exists over module scopes, set it to TRUE to avoid it to be deleted when terminating current script.

 

The second syntax will create an array with the same dimensions as the supplied array or reference to array.  Note; This will only work on arrays.

 

Example:

 

DIM ptr AS REFERENCE

 

' Note; Need to UNREF before we can allocate a new variable or array

'       otherwise the referenced variable/array will be used

 

ptr = NEW( REAL, 60, 4, 3 )

UNREF ptr

 

ptr = NEW( INTEGER, 600, 10, "Kalle" )

UNREF ptr

 

ptr = NEW( INTEGER, 600, 10, "Kalle", TRUE )

UNREF ptr

 

ptr = NEW( STRING, 60 )

UNREF ptr

 

ptr = NEW( REAL )

ptr = 1.234

UNREF ptr

 

ptr = NEW( INTEGER )

ptr = 69

UNREF ptr

 

ptr = NEW( STRING )

ptr = "Kalle Anka likes Nils"

UNREF ptr

 

' Example 2 - Using NEW( AS, ref ) together with array of reference

 

DIM ref_arr( 2 ) AS REFERENCE

DIM r AS REFERENCE

 

r = NEW( REAL, 20, 4, 3 )

 

ref_arr( 1 ) = REF( r )

ref_arr( 2 ) = NEW( AS, r )

 

MAL.Set( ref_arr( 1 ), 1.1 )

MAL.Set( ref_arr( 2 ), 2.2 )

 

RUN( "ArrayTable", ref_arr( 1 ), TRUE )

RUN( "ArrayTable", ref_arr( 2 ), TRUE )

 

UNREF ref_arr( 1 )

UNREF ref_arr( 2 )

UNREF r

 

END