in Delphi the procedure write can handle:
write(TF,st1)
and
write(TF,st1,st2,st3,st4);
I want to declare a procedure that can also do that, what is the syntax?
and the option of:
write(TF,[st1,st2,st3])
is less desirable, though I know how to do that.
the main purpose was to pass ShortString
s into function, that would make a read call from file, and would read at the length of the shortString
as defined. however after passing it as variant or in open array the shortString
loses its "size" and become 255, which making this pass unusable, for me.
but the answer is still got if you want to pass open array.
First of all Inc
and Write
are bad examples because they both get special treatment from the compiler. You can't write a function that behaves exactly like those two do yourself. There are alternatives you should investigate.
You can create multiple versions of your method using varying number of parameters, and varying types. Something like this:
procedure MyInc(var i:Integer); overload;
procedyre MyInc(var i:Integer; const N:Integer); overload;
procedure MyInc(var i:Integer; const N1, N2: Integer); overload;
procedure MyInc(var i:Integer; const N1, N2, N3: Integer):overload;
This is feasible if the required number of overloads is not that large. The compiler would probably handle lots of overloads easily, but you'd probably not want to write them. When the number of overloads becomes a problem you can switch to arrays:
A function can take a parameter of type array of YourType
, and when you call that function you can pass as many parameters as you might need:
procedure MyInc(var i:Integer; Vals: array of Integer);
And then use it like this:
MyInc(i, []); // no parameters
MyInc(i, [1]);
MyInc(i, [1, 34, 43, 12]);