[ACCEPTED]-Can I assign items in an array all at once in Delphi/Pascal?-syntax
When it comes to dynamic arrays, yes:
type
TIntArray = array of integer;
procedure TForm1.Button1Click(Sender: TObject);
var
MyArr: TIntArray;
begin
MyArr := TIntArray.Create(10, 20, 30, 40);
end;
When 4 it comes to static arrays, you need to write 3 a helper function:
type
TIntArray = array[0..2] of integer;
function IntArray(const A, B, C: integer): TIntArray;
begin
result[0] := A;
result[1] := B;
result[2] := C;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyArr: TIntArray;
begin
MyArr := IntArray(10, 20, 30);
end;
This resembles how the 2 Point
function creates a TPoint
record. (Records and 1 arrays are not the same thing, though.)
This is an area where Delphi turns something 6 that is a simple one-line assignment statement 5 in most languages into something more complicated.
One 4 approach would to declare the value as a 3 typed constant:
type
HSVRealArray = array[1..3] of real;
const
constHSVVal: HSVRealArray = (0, 0, 0);
var
currentValue: HSVRealArray;
begin
currentValue := constHSVVal;
end;
Another approach is to create 2 utility functions that return the type you 1 need:
function MakeHSVRealArray(H, S, V: Real): HSVRealArray;
begin
Result[1] := H;
Result[2] := S;
Result[3] := V;
end;
currentValue := MakeHSVRealArray(0,0,0);
With some extra work, you can achieve a 1 clean implementation:
var
x: TRGB;
begin
x := TRGB.Init(0,0,0);
end;
TRGB = record
Red, Green, Blue: real;
class function Init(r,g,b: real): TRGB; static;
end;
class function TRGB.Init(r, g, b: real): TRGB;
begin
Result.Red := r;
Result.Green := g;
Result.Blue := b;
end;
For array handling, array initialization 6 and array constant declarations, Delphi 5 doesn't make simple things simple.
In some 4 situations similar to yours, I initialize 3 the array with a utility function taking 2 one open array parameter and returning the 1 appropiate static string.
const
MaxArray = 10;
type
TRealStaticArray = array[0..MaxArray] of Real;
function RealArray(const AnArray: array of real):TRealStaticArray;
const DefaultValue=0.0;
var i: integer;
begin
// EDIT: commented out, thanks Serg. for i:= 0 to low(AnArray)-1 do result[i]:=DefaultValue;
for i:= High(AnArray)+1 to MaxArray do
result[i]:=DefaultValue;
for i:= Low(AnArray) to High(AnArray) do
if (i>=0) and (i<=MaxArray) then
result[i]:=AnArray[i];
end;
Use it this way:
var MyArray: TRealStaticArray;
...
MyArray := RealArray([10.0, 20.0, 30.0]);
Delphi-XE7 introduced a new syntax for dynamic array
initialization.
// compile time const declaration of dynamic array
const
my_ConstArray_name: TArray<Integer> = [128, 38459, 438, 23674];
// compile time var declaration of dynamic array
var
my_VarArray_name: TArray<Integer> = [128, 38459, 438, 23674];
Runtime 3 assignment of dynamic arrays:
var
a : TArray<Integer>;
begin
a := [1,2,3];
Unfortunately 2 this syntax cannot be used on ordinary static 1 arrays:
Type
TMyArray = array[0..3] of Integer;
const
cMyArray: TMyArray = [0,1,2,3]; // E2010 Incompatible types: 'TMyArray' and 'Set'
cMyArray: TMyArray = (0,1,2,3); // Works, as in all Delphi versions
var
MyArray: TMyArray;
begin
// This fails as well
MyArray := [0,1,2,3]; // E2010 Incompatible types: 'TMyArray' and 'Set'
MyArray := (0,1,2,3); // E2029 ')' expected but ',' found
//-----------^-------
// This works in all Delphi versions !
MyArray := cMyArray;
Can you try something like that:
TRGB = record
Red : integer;
Green: integer;
Bklue: integer;
end;
var Variable:TRGB;
Variable.Red:=0;
Variable.Green:=0;
Variable.Blue:=0;
0
I know this is an old post, but I came across 7 this while looking into the auto-assignment 6 technique for Delphi. This post has a very 5 nice explanation, along with a way to auto-assign 4 an array of records:
http://delphi.about.com/od/adptips2006/qt/const_array.htm
Unfortunately, the initialization 3 can only be done with native or record types. Class 2 objects need a helper function as others 1 have shown above.
Putting aside the general question, I'd 15 like to point out that the specific use-case 14 was never going to work anyway: Delphi can't 13 return on the stack the contents of an array 12 of unknown length.
The return type of a 11 function needs to be known by the calling 10 code. It may be a pointer to the heap: it 9 may even be a memory-managed object on the 8 stack: but it is not permitted to be an 7 undefined number of bytes on the stack, decided 6 dynamically by the target function.
And in 5 many other languages, the example
return (0,0,0) //return pointer to automatic allocation
would be 4 undefined, even if the compiler lets you 3 do it.
A common method of defining, using 2 and returning const values is
int myarray[8] = {128, 38459, 438 ... 23674};
memcpy (myparam, myArray, sizeof (myArray));
... and you 1 can do that in Delphi
[0, 0, 0]
is a set in Delphi. (0, 0, 0)
is an array constant 3 in Delphi! Just use:
HSVtoRGB := (0, 0, 0);
(Unless HSVtoRGB is 2 a dynamic array. Did you declare it as var HSVtoRGB: array[1..3] of integer;
or 1 something similar?
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.