Variable size Type array?

BlitzMax Forums/BlitzMax Beginners Area/Variable size Type array?

JoshK(Posted 2006) [#1]
Is there any way to make a list of variable length as a parameter to a type?

Type Model
Field Children[]
EndType


N(Posted 2006) [#2]
Use a linked list.

Type Model
    Global Root:TList = New TList
    
    Field Children:TList = New TList
    Field Parent:Model
    Field Link:TLink
    
    Method AddChild( c:Model )
        If c Then c.SetParent( Self )
    End Method

    Method GetChild:Model( idx% )
        Return Model(Children.GetValue( idx )) ' Cast from Object to Child on return
    End Method
    
    Method SetParent( p:Model )
        If Link Then Link.Remove( )
        
        Parent = p
        
        If p Then
            Link = p.Children.AddLast( Self )
        Else
            Link = Root.AddLast( Self )
        EndIf
    End Method
End Type



Azathoth(Posted 2006) [#3]
Doesn't that already work? Just use New when you want to give it a size.


Who was John Galt?(Posted 2006) [#4]
Yeah you declare arrays with new and assign them to your variable. Can't remember the syntax off the top of my head.


LosButcher(Posted 2006) [#5]
[Code]SuperStrict

Type Model
Field myChildren:Children[]
EndType

Type Children
Field something:Int
EndType

Local myModel:Model = New Model

myModel.myChildren = New Children[8]
myModel.myChildren = myModel.myChildren[..5]

Print myModel.myChildren.length[/Code]
Might have overdone this - but as Azathoth and Nomen luni said, just use new.


N(Posted 2006) [#6]
Using arrays for this is a bad idea. The time it takes to resize an array is variable, adding a link to a linked list is near constant and far more efficient.