Posted By

xyzeugene on 08/21/15


Tagged


Versions (?)

Delphi/Lazarus: function accepting a variable # of parameters


 / Published in: Delphi
 

Variable # of parameters, TVarRec

Thanks, here's an example of a function accepting a variable # of parameters for anyone interested:

  1. function Add(const Args:array of const):string;
  2. var I,N:integer;
  3. ArgsI:TVarRec;
  4. S:string;
  5. begin
  6. N:=High(Args);
  7. S:='';
  8. for I:=0 to N do
  9. begin
  10. ArgsI:=Args[I];
  11. case ArgsI.VType of
  12. vtInteger: S:=S+IntToStr(ArgsI.VInteger);
  13. vtBoolean: if(ArgsI.VBoolean)then S:=S+IntToStr(1) else S:=S+IntToStr(0);
  14. vtChar: S:=S+ArgsI.VChar;
  15. vtString: S:=S+ArgsI.VString^;
  16. end;
  17. end;
  18. Add:=S;
  19. end;
  20.  
  21. procedure Test;
  22. var C:string;
  23. begin
  24. C:=Add([false,true,2,'-','Three']);
  25. end;
  26. C='012-Three'

Report this snippet  

You need to login to post a comment.