Hi!
As a complete newbie at BlitzMax I've been trying to get a hang of conventions and patterns to use. So this is a little thing I've encountered several times:
The TList class has a .ToArray() method that returns an array of Object (Object[]). Problem is that arrays in BlitzMax seem typed to their base type and not compatible with arrays of another type (if i grasp things right). I've found no direct way of making Object[] to become a String[] without making a new array of the desired type and copy the data in. (i.e. casting did not work out for me)
So I've written this little function below to at least automate the copying part for me, but my question to the pro BlitzMax developers out there is: Is there an even easier way to do this or is the copying the way to go?
edit: spelling
As a complete newbie at BlitzMax I've been trying to get a hang of conventions and patterns to use. So this is a little thing I've encountered several times:
The TList class has a .ToArray() method that returns an array of Object (Object[]). Problem is that arrays in BlitzMax seem typed to their base type and not compatible with arrays of another type (if i grasp things right). I've found no direct way of making Object[] to become a String[] without making a new array of the desired type and copy the data in. (i.e. casting did not work out for me)
So I've written this little function below to at least automate the copying part for me, but my question to the pro BlitzMax developers out there is: Is there an even easier way to do this or is the copying the way to go?
'*********************************************************************** ' convertArrayType() ' ' Purpose: ' Converts an array of object (Object[]) to an array of another type ' (choosen by user). ' ' The resulting array is an array of the type of the sent in ' referenced object targetTypeForArray. ' Function convertArrayType:Object(inArray:Object[], targetTypeForArray:Object) Assert Object[] (inArray), "inArray must be array of object type" Local newArrayType:TTypeId = TTypeId.ForObject(targetTypeForArray).ArrayType() Local result:Object = newArrayType.NewArray(inArray.length) For Local idx:Int = 0 To inArray.length - 1 newArrayType.SetArrayElement(result, idx, inArray[idx]) Next Return Object(result) End Function 'Example usage: Local strList:TList = New TList strList.AddFirst("String#1") strlist.AddFirst("String#2") Local strArr:String[] = String[] (convertArrayType(strList.ToArray(), ""))
edit: spelling