make a procedure with unspecified nuber of parmeters

is there any way to make a procedure like readln or writeln that are'nt limited in the nubers of parameters.
LVL 2
saar071697Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
smartkidConnect With a Mentor Commented:
if your are using Delphi, you can simply declare your procedure called const array parameter as the following:

procedure NoParamLimits(const args : array of const)
begin
  ....
end;

you can detect which type of each parameter is by calling the args[i].VType and then use a typecast to convert each argument into prefer type. Bellow is an example:

procedure DecodeBools(const bools : array of const; Value : Integer);
type
  PBoolean = ^Boolean;
var
  i : Integer;
  p : PBoolean;
begin
  for i := 0 to High(bools) do
  begin
    Assert(bools[i].VType = vtPointer);
    p := bools[i].VPointer;
    p^ := Boolean((Value and (1 shl i)) shr i);
  end;
end;
0
 
graham_kCommented:
I can tell you how to do it in C. So, if no-one else comes up with an answer, maybe you can code a separate unit in C & link it in with your Pascal?

Sorry not to be of more help, but I don't know how to do it in Pascal.
0
 
nricoCommented:
I don't think it is possible. At least not in Pascal.

What you *could* do to emulate this (but it's a lot of work) is

Type
  PExtParam=^TExtParam;
  TExtParam=Record
    Id:Word;
    Data:Pointer;
[... Next:PExtParam...]
  End;

Then you could (just as you wish) pass a dynamic array or a linked list of these records.
The Id could be something like ID_STRING or ID_INTEGER, which tells the procedure how to interpret the value.

If you used a linked list that ultimately gets destroyed in the called function, you could then do something (assuming Pa(id,data,next) is a function that adds a record to a list and returns a pointer to the record) like:

MyString:='The value of MyInteger is: ';
String2:='. Have a nice day!';
MyInteger:=33;
MyFunction(
  Pa(ID_STRING,@MyString,
  Pa(ID_INTEGER,@MyInteger,
  Pa(ID_STRING,@String2,
  Nil
  ))));

But as I said, it's a lot of work and I don't know if you want to go to such an extent.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
smartkidCommented:
About is a routine to decode an Integer into an array of boolean. In fact, each element in the array do not have to be the same type. You can combine strings, Integers, Floats, etc, in one array and pass the array as a hole to a procedure. It's a very useful tech.
0
 
graham_kCommented:
well, if you _really_ want to do it the hard way - the caller allocates a block of memory. The first 4 bytes hold the length of the memory block. Tehn come a series of entries. Each has one byte to say what type of variable - char, int, etc, then a veriable number of bytes for the data (strings would have length as byte zer0, just like normal Pascal strings).

The caller apsses a pointer to this memory & the routine decodes it & frees teh memory.
0
 
My name is MudSystems EngineerCommented:
The Answer is NO WAY... :(
0
 
nricoCommented:
Yeah, whatboy, but he didn't come here to hear that ;-).
Though Pascal doesn't extended parameters by default, the functionality _can_ be emulated in a number of ways, mine and graham's answers as examples.
0
 
My name is MudSystems EngineerCommented:
The Answer is NO WAY... :(
0
 
graham_kCommented:
hey, nrico, I must have missed your comment when posting mine. It's basically the same thing. So, if saar really want to do it, he must follow that method. You can have the points.
0
 
nricoCommented:
Stubborn guy! >-(

saar, if you want I can write some sample code (or a unit).
0
 
My name is MudSystems EngineerCommented:
;)
0
 
nricoCommented:
<-)
0
All Courses

From novice to tech pro — start learning today.