Reputation: 2383
I want to pass multiple objects as one parameter with the smallest effort.
I've got some type
TOpenMode = [omNew, omEdit, omBrowse]
And a procedure
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: TUniDataSet; TableOpenMode: TOpenMode);
I want to pass more than one dataset. Can I do that without arrays or creating new objects? How can I make them to be passed in pairs [UniTable1, TOpenMode], [UniTable2, TOpenMode]
?
Upvotes: 2
Views: 956
Reputation: 613352
The simplest way to combine multiple objects in a single compound type is a record:
type
TDataSetAndOpenMode = record
DataSet: TUniDataSet;
OpenMode: TOpenMode;
end;
For convenience provide a function to initialise one of these records:
function DataSetAndOpenMode(DataSet: TUniDataSet;
OpenMode: TOpenMode): TDataSetAndOpenMode;
begin
Result.DataSet := DataSet;
Result.OpenMode := OpenMode;
end;
Then your OpenForm
function can receive an open array of such records:
procedure OpenForm(Form: TForm; const Datasets: array of TDataSetAndOpenMode;
NewWindow: Boolean=False);
Note that I have put the NewWindow
parameter at the end. Since it has a default value, that default value is only useful when it appears at the end of the list.
Now, to call the function you can write code like this:
OpenForm(Form, [DataSetAndOpenMode(DataSet1, OpenMode1),
DataSetAndOpenMode(DataSet2, OpenMode2)]);
Upvotes: 5
Reputation: 19356
If you want to pass multiple pairs as one parameter, I don't see how you can avoid declaring at least a record to define the pair and at least an open array parameter to pass multiple instances of those records as one parameter.
type
TDatasetModePair = record
DS: TUniDataSet;
Mode: TOpenMode;
end;
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: array of TDatasetModePair);
But you'll probably find that it will be much easier to declare your own array type:
type
TDatasetModePairArray: array of TDatasetModePair;
the procedure declaration then becomes:
procedure OpenForm(Form: TForm; ANewWindow: boolean = false;
Datasets: TDatasetModePairArray);
Regardless of that though, there is no way around having to create the array before you can pass it to your function:
var
MyArray: TDatasetModePairArray;
begin
SetLength(MyArray, 2);
MyArray[0].DS := SomeDataSet;
MyArray[0].Mode := omEdit;
MyArray[1].DS := SomeOtherDataSet;
MyArray[1].Mode := omBrowse;
Upvotes: 3