The NEW operator allocates some memory to hold a user defined type structure within. The allocator returns the types memory handle, which can then be assigned to Type Variable / Typed Array, Linked List variables and typed pointers. So we use the NEW operator to fill in our typed arrays containers and lists, which are empty once they're dimensioned.
FACTS:
* We recommend reading up on Types Tutorial, LinkedLists and Pointers also
* The runtime system behind PlayBASIC programs includes automatic type allocation by default. This system projects the user from potential write errors when writing to typed variable and arrays, but we actually recommend you use the New operator to allocate each type and store them yourself.
Mini Tutorial (Typed Variables):
In this (Cut and paste friendly) example we're declaring our person type, making two typed variables called Billy and Sally, then using NEW to allocate the required memory for each.
Note: When we assign a newly allocated type to a typed variable / typed array, this will overwrite whatever was previously created in the variable or array.
; Declare the "Person" user defined type. Type Person ; These Fields will hold this persons full name FirstName$ SurName$ EndType ; Dimension Billy of type Person, Dim Billy As Person ; Init Billy CreatePerson(Billy(),"Billy","Citizen") ; Dimension Sally of type Person, Dim Sally As Person ; init Sally CreatePerson(Sally(),"Sally","Stevens") ; Display Sally ShowPerson(Sally()) ; Display Sally ShowPerson(Billy()) ; display the screen and wait for a key press Sync WaitKey Function CreatePerson(Me.Person,FirstName$,LastName$) ; Use New to Alloc a Fresh new person and assign it ; to the passed type variable called ME ; This allocation will overwrite any previous data ; in this type.. Me= New Person ; Set the name fields Me.FirstName$ =FirstName$ Me.Surname$ =LastName$ EndFunction Function ShowPerson(Me.Person) Print Me.FirstName$+" "+Me.Surname$ EndFunction |
This example would output. Sally Stevens Billy Citizen |
Mini Tutorial (Typed Arrays):
In this (Cut and past friendly) example we're declaring our person type, making a typed array called Friends, then using NEW to allocate the required memory.
Note: When we make assignments to a typed array cells this will overwrite whatever was in the typed cell previously.
; Declare the "Person" user defined type. Type Person ; These Fields will hold this persons full name FirstName$ SurName$ EndType ; Dimension Friends array of type Person, ; this array is big enough to hold 10 people Dim Friends(10) As Person ; Init Billy are Index #1 of our friends array CreatePerson(1,"Billy","Citizen") ; Init Billy at Index #2 of our friends array CreatePerson(2,"Sally","Stevens") ; Display my friends For lp=1 To GetArrayElements(Friends(),1) ; Check if this friend exists or not ? If Friends(lp)<>0 ; This person exists, so lets display them Print Friends(lp).FirstName$+" "+Friends(lp).Surname$ EndIf Next ; display the screen and wait for a key press Sync WaitKey Function CreatePerson(Index,FirstName$,LastName$) ; Alloc a New person and store them at this ; position with within the array Friends(index) = New Person ; Set the name fields Friends(index).FirstName$ =FirstName$ Friends(index).Surname$ =LastName$ EndFunction |
This example would output. Billy Citizen Sally Stevens |
Mini Tutorial (linked Lists):
In this (Cut and past friendly) example we're declaring our person type, making your Friends list and then adding two people to the Linked list.
You'll notice that
; Declare the "Person" user defined type. Type Person ; These Fields will hold this persons full name FirstName$ SurName$ EndType ; Dimension the Friends variable of type Person, ; with linked list support Dim Friends As Person List ; Add a person to Friends list Friends= New Person ; The Friends list is now pointing at the newly added ; Person, so lets assign this persons name Friends.FirstName$ ="Billy" Friends.Surname$ ="Citizen" ; Add another person to Friends list Friends= New Person ; The Friends list is now pointing at the newly ; added person, so assign this persons name also Friends.FirstName$ ="Sally" Friends.Surname$ ="Stevens" For Each Friends() Print Friends.FirstName$+" "+Friends.Surname$ Next ; display the screen and wait for a key press Sync WaitKey |
This example would output. Sally Stevens Billy Citizen |
Mini Tutorial (Typed Pointers):
In this (Cut and past friendly) example we're declaring our person type, making two typed pointer called Billy and Sally, then using NEW to allocate the required memory for each.
; Declare the "Person" user defined type. Type Person ; These Fields will hold this persons full name FirstName$ SurName$ EndType ; Dimension Billy pointer of type Person Dim Billy As Person Pointer ; Init Billy Billy=CreatePerson("Billy","Citizen") ; Dimension Sally pointer of type Person, Dim Sally As Person Pointer ; init Sally Sally=CreatePerson("Sally","Stevens") ; Display Sally ShowPerson(Sally) ; Display Billy ShowPerson(Billy) ; display the screen and wait for a key press Sync WaitKey Function CreatePerson(FirstName$,LastName$) ; Use New to Alloc a Fresh new person and assign it ; to the passed type variable called ME Me= New Person ; Set the name fields Me.FirstName$ =FirstName$ Me.Surname$ =LastName$ EndFunction Me As Person Pointer Function ShowPerson(Me As Person Pointer) Print Me.FirstName$+" "+Me.Surname$ EndFunction |
This example would output. Sally Stevens Billy Citizen |
|