English 中文(简体)
How set dynamic array size in Oxygene (SetLength doesn t work)
原标题:

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;




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签