Tutorial :How set dynamic array size in Oxygene (SetLength doesn't work)



Question:

What is the equivalent of SetLength using Oxygene? I'm trying to size an integer array.

var listIndexes: array of integer;  begin       setLength(listIndexes,5);  // doesn't work  end;  


Solution:1

Bill, the function Setlength does not exist in Delphi-Prism (you can use the namespace ShineOn.Rtl from ShineOn wich have a partial implementation of the Setlength function).

In delphi prism you can try this

type  IntegerArray = array of integer;    var listIndexes: IntegerArray;  listIndexes:=New IntegerArray(5);  

or

 var listIndexes: Array of Integer;   listIndexes:=New Integer[5];  

UPDATE

Also, you can write your own SetLength

method SetLength(var myArray: Array of Integer; NewSize: Integer);  var   ActualLength: Integer;  begin    var DummyArray: &Array := &Array.CreateInstance(typeOf(Integer), NewSize);    if assigned(myArray) then    begin      ActualLength:= iif(myArray.Length > NewSize, NewSize, myArray.Length);      &Array.Copy(myArray, DummyArray, ActualLength);    end;    myArray := array of integer(DummyArray);  end;  


Solution:2

[edit updated] You can use the Array.Resize method which seems to apply only to .NET Framework 3.5 applications. The equivalent C# code looks like this:

    // Create and initialize a new string array.      String[] myArr = {"The", "quick", "brown", "fox", "jumps",           "over", "the", "lazy", "dog"};        // Display the values of the array.      Console.WriteLine(           "The string array initially contains the following values:");      PrintIndexAndValues(myArr);        // Resize the array to a bigger size (five elements larger).      Array.Resize(ref myArr, myArr.Length + 5);   

However if you are targeting earlier versions of the .NET Framework or require very frequent resize changes to the list then I would recommend that you use the .NET ArrayList or another of the System.Collections in .NET which may well make your code considerably simpler and allow you to use new features of the Framework such as Linq.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »