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;
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;
[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.
This is just a quick update
RemObjects has added a cool new VCL framework that can be used on top of both .net and their new native targets. This also affect strings.
Strings outside of either .net or the VCL are not mutable. That's why they can only be sized down (String.Substring), which is what you are experiencing in your question.
However, it's fairly easy to make your own "setlength()" method. It's not pretty to look at and very old-school, but for an immediate solution, it does the job.
I use a lookup-table so that we grow strings by a factor of 16. The reminder (less than 16) likewise use the lookup table. LLVM optimizes code like this aggressively. So ugly it might be, but it works. Hope this helps. Just rename as you see fit:
var str_growth_LUT: array[1..16] of String
= [' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' '];
class procedure QTXString.SetLength(var Text: String; NewLength: Integer);
begin
var lCurrent := length(Text);
if NewLength < lCurrent then
Text := Text.Substring(1, NewLength)
else
if NewLength > lCurrent then
begin
var diff := NewLength - lCurrent;
var temp: String;
// Calculate parts of 16
var major := diff mod 16;
var minor := diff - (major * 16);
// grow by chunks of 16
while major > 0 do
begin
temp := temp + str_growth_LUT[16];
dec(major);
end;
// Add reminder (less than 16)
if minor > 0 then
temp := temp + str_growth_LUT[minor];
Text := temp;
end;
end;
精彩评论