Reputation: 47
Although I found some postings concerning the subject setlength I couldn't find out a solution for my problem:
With the code below - which is part of a much bigger program -
type TVektor2=array[20] of extended;
TElement2=array[20] of String;
Procedure Sort_Shell2(
element1X: TElement2; zahlX: TVektor2; var Element2X : TElement2;
var zahl2X : TVektor2);
var
bis, i, j, k, min : LongInt; l, laenge : single;
h,s,w,h1,h2, ElemX: string;
e : array[20] of String;
begin
laenge := 5; // just an example
SetLength(Element1X, 3); /// Error
//DynArraySetlength(e,l,1); /// how?
bis := High(e);
k := bis shr 1;// div 2
while (k > 0) do
begin
for i := 0 to (bis - k) do
begin
j := i;
h1 := e[j]; //I use this because before I had an Acces violation
h2 := e[j + k]; // using directly e[j] := e[j+k];
while (j >= 0) and (h1 > h2) do
begin
h := h1;
l:=zahlx[j]; //str(l:5:3,S);showmessage(h + s);
e[j] :=e[j + k];
zahlx[j] := zahlx[j+ k];
e[j + k] := h;
zahlx[j+ k]:=l;
if j > k then
Dec(j, k)
else
j := 0;
end; // {end while]
end; // { end for}
k := k shr 1; // div 2
end; // {end while}
Element2x:=e; zahl2x :=zahlx;
end;
I get the error 'incompatible types' if I try the setlength command like this. I tried - with a for next loop -to attribute to each position of the static array (with 20 entries) or also to the correponding dynamic array and then to use setlength. But it didn't work. Is there some casting possible to transform TElement2 to an array ? (since it is already an array!)
Why isn't it possible to use a simple static array[1..20] of strings = a, set for each position a[i] = TElement2[i] and use setlength(a,5)?
If I use DynArraySetLength(Pointer, typeInfo,dimCnt, lengthVec) what must I use for these variables? I don't know nearly anything about Pointers and I have no idea for such problem what parameters I must use to get an array of a given length starting with the given TElement2 array. By the way, in general, is it a good idea to use dynamic arrays?
By the way there might also be an error in this sorting routine because it doesn't work well...
Can any one help me?
Upvotes: 1
Views: 538
Reputation: 1002
In order to use a Dynamic Array in Delphi you have to declare an array like this:
TElement2=array of String;
and not TElement2=array[20] of String
or TElement2=array[1..20] of String;
If you declare TElement2
that way then SetLength(element1X, 3);
is going to work.
moreover when you assign at the bottom of the code
Element2x:=e;
it's not going to compile unless both variable aren't declared of the same type:
e : TElement2;
Upvotes: 3