How to convert from types into an array

BlitzMax Forums/BlitzMax Programming/How to convert from types into an array

Hiya,

How do you convert from types to arrays, and is the data still accessible?

Can you also access the fields just the same?

Can I destroy this array, and if so, how?

What if I need to "refresh" the array with contents from types (think of an editor) - will this lead to memory leaks? The Types will be different sizes each time.

Many thanks!

I think you mean lists. Types are just like varibles in BMax meaning you can just do this:
Type myType
	
	Function Create:myType()
		
		Local newType:myType = New myType
		Return newType
		
	End Function
	
	Method New()
		
		x% = Rand(0, 100)
		y% = Rand(0, 100)
		
	End Method
	
	Field x%, y%
	
End Type

Global myTypeList:TList = CreateList()
myTypeList.AddLast myType.Create()

' Type cast
Print myType(myTypeList.Last()).x + ", " + myType(myTypeList.Last()).y

' Convert to an array
Local myTypeArray:Object[] = ListToArray(myTypeList)

' No need for type cast
Print myType(myTypeArray[0]).x + ", " + myType(myTypeArray[0]).y


hope this helps a little.

Thanks - it does. Does wrapping it in my Type cause a performance hit? For example myType(myTypeArray[0]).x verses myTypeArray[0].x, which is possible if I just make my own arrays and copy a list manually?

Also, how do I resize and remove an array once I have it?

you can resize using slicing... there should be somthing about it in the docs:
Local a[200]	'initialize a[] to 200 elements
a=a[50..150]	'extract middle 100 elements of a[]
a=a[..50]     	'extract first 50 elements of a[]
a=a[25..]	'extract elements starting from index 25 of a[]
a=a[..]		'copy all elements of a[]
a=a[..200]	'resize a[] to 200 elements

taken from the docs...

to get rid of an array you could just set it to null
myArray = Null


i'm not sure about type casting hinders perforamce IMO i don't think it does.

Thank you very much :)

How do you remove an element from the middle of an array? Say my array size is 100 and I want to get rid of item 53. How do I do that?

How do I do that?
It depends on whether you want empty places in your array.

If you do, you just go array[53] = null.

If you don't you need to either:
* Swap it with the last element, and cut the last element off the array.
* "Compact" the array, which usually involves traversing the array, and moving elements down when a null reference is found.
* Use lists instead.