views:

648

answers:

2

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

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

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;
RRUZ
That creates a new array each time. Can't arrays be resized anymore?
Rob Kennedy
Yes, but only with .NET 3.5 - The Collections are the preferred way to do Arrays now Rob. :)
jamiei
+1  A: 

[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.

jamiei