1

So I have this method that I should call, and it takes an array constructor as an argument, as shown here:

myTTable.InsertRecord([var1, var2, var3]);

The problem is that I have a very big number of variables that I managed to put into an array of strings params. But I seem unable to pass it as an argument.

All these do not work :

myTTable.InsertRecord(params);
myTTable.InsertRecord([params]);
myTTable.InsertRecord(Slice(params, 88)); // I know I have 88 elements

The only way I found is to write :

myTTable.InsertRecord([params[0], params[1], params[2], ... params[87]]);

Which I prefer not to do. Is there another way?

Also, I'm using a pretty old version of Delphi and I cannot upgrade for some corporate reasons.

3
  • Please show the signature of InsertRecord and the type of params Commented Jul 17, 2019 at 14:30
  • @StefanGlienke: docwiki.embarcadero.com/Libraries/Rio/en/… Commented Jul 17, 2019 at 15:40
  • Thanks @Uwe for notifying me on my mistake. I thought myTTable was OP's own invention. Commented Jul 17, 2019 at 16:37

1 Answer 1

2

InsertRecord() takes a Variant Open Array (array of const) as input. The typical way to construct such an array in Delphi is using the bracket syntax around every individual value, as you already discovered.

However, an array of const is really an array of TVarRec, which you can build up manually (with caution! As TVarRec has gotchas in how certain types, like strings, have to be passed in it).

For example:

procedure DoInsert(params: array of string);
var
  v: array of TVarRec;
  i: Integer;
begin
  SetLength(v, Length(params));
  for i := 0 to Length(params)-1 do
  begin
    {$IF CompilerVersion >= 20}
    // Delphi 2009 and later...
    v[i].VType := vtUnicodeString;
    v[i].VUnicodeString := Pointer(params[i]);
    {$ELSE}
    // Delphi 2007 and earlier...
    v[i].VType := vtAnsiString;
    v[i].VAnsiString := Pointer(params[i]);
    {$IFEND}
  end;
  myTTable.InsertRecord(v);
end;

var
  params: array of String;
begin
  SetLength(params, 88);
  // populate params as needed...
  DoInsert(params);
end;
Sign up to request clarification or add additional context in comments.

3 Comments

I have a hard time compiling this, i've just checked and it turned out to be Delphi 2. it says "incompatible types string and array" on using SetLength, same thing if I change Length(params) to 88. I think it doesn't even recognize that $IF
@FiRas what problem are you having exactly? You need to be more specific. Is it the {$IF} statements? {$IF} and CompilerVersion were introduced in Delphi 6. Just remove those statements and use the vtAnsiString block for Delphi 2 (AnsiString was introduced in Delphi 2, UnicodeString in Delphi 2009).
@FiRas dynamic arrays are introduced in Delphi 4. So for Delphi 2, you will have to allocate the arrays manually. Does Delphi 2 have GetMem() or AllocMem() available? However you manage to allocate the string array, you will have to do the same thing for the TVarRec array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.