Basic Nested Types Demo


(Posted 1 year ago) RonTek

snippet example showing how to use nested types or deriving from base types/class

Cls
Graphics 800,600,0,2

SeedRnd MilliSecs()

Type vehicle
    Field id
    Field year
    Field type_$
End Type

Type car
    Field auto.vehicle
    Field model$
    Field transmission$
End Type

Type plane
    Field jet.vehicle
    Field model$
    Field propulsion$ 
End Type

For i = 1 To 2  
    street.car = New car
    street\transmission$ = "Manual"
    street\auto = New vehicle
    street\auto\year = Rnd(8)+2010
    street\auto\type_$ = "Sports"   
    street\auto\id = Rnd(1000)+20000
Next

For i = 1 To 2  
    military.plane = New plane
    military\propulsion$ = "Gas Turbine"
    military\jet = New vehicle
    military\jet\year = Rnd(10)+1990
    military\jet\type_$ = "Fighter" 
    military\jet\id = Rnd(1000)+20000
Next

; show plane types only which has a unique field propulsion$ and a base/common field (id and year)

Print "Jets Only"
Print "========="
For fighter.plane  = Each plane
 Print fighter\jet\id + ": " + fighter\propulsion$  + " ("+fighter\jet\year+")"
Next

Print ""

; show all vehicle objects created which we can access the base/common fields (id, year and type_$)

Print "All Vehicles"
Print "============"
For all.vehicle  = Each vehicle
 Print all\id+ ": " + all\type_$ + " ("+all\year+")"
Next

Repeat
Flip
Until KeyHit(1)

Reply To Topic

Please log in to reply