I will support and port a bunch of very old Delphi code in Delphi XE2 that is full of VarArrayCreate to fake dynamic arrays having a lower bound that is not zero.
Disadvantages of using Variant types:
- rather slowly than native arrays (the code performs many complex financial calculations, so speed is important)
- not a safe type (especially when the wrong
var... constant is accidentally used, and the Variant system starts doing unwanted conversions or rounding)
Both can become controversial if I can use dynamic arrays .
The good thing about variant arrays is that they can have nonzero lower bounds.
What I recall is that dynamic arrays always started from the bottom of zero.
Is this still true? In other words: is it possible for dynamic arrays to start at a different boundary than zero?
As an illustration, the example before / after for a specific case (one-dimensional, but the code is full of multidimensional arrays and, in addition, varDouble , the code also uses various other varXXX data types that TVarData allows to use):
function CalculateVector(aSV: TStrings): Variant; var I: Integer; begin Result := VarArrayCreate([1,aSV.Count-1],varDouble); for I := 1 to aSV.Count-1 do Result[I] := CalculateItem(aSV, I); end;
The CalculateItem function returns Double . Borders from 1 to aSV.Count-1 .
The current replacement is this: trading the spatial zero element of the result to improve compile-time checking:
type TVector = array of Double; function CalculateVector(aSV: TStrings): TVector; var I: Integer; begin SetLength(Result, aSV.Count); // lower bound is zero, we start at 1 so we ignore the zeroth element for I := 1 to aSV.Count-1 do Result[I] := CalculateItem(aSV, I); end;