Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays as parameters in a procedure

Status
Not open for further replies.

cpmasesa

Programmer
Oct 24, 2002
78
AE
Hi,

I have the following function:

procedure TfrmReports.ssShowReport(sReportFile : string; aReportDataSet : TADOQuery; arVariables, arValues : Array of string);
var sAppPath : string; i : integer;
begin
sAppPath := ExtractFilePath(ParamStr(0));
dsReports.DataSet := aReportDataSet;
aReport.LoadFromFile(sAppPath + '\Reports\' + sReportFile + '.frf');
for i := Low(arVariables) to High(arVariables) do
SetVariableValues(arVariables, arValues); //set an individual variable
aReport.ShowReport;
end;

I would like to pass ONE array that has pairs of 'Variable' and 'Variable value' (2 dimensional array?) instead of the two SEPARATE arrays.

How can i do that??

TIA,

BTW, thanks to Andrew for help on the algorithm for combinations.

cpmasesa
 
You could use an array of records, by declaring the following in the "Interface" section of your unit:
Code:
type
  VariableRecord = record
    Variable: String;
    VariableValue: String;
  end;
Then in a routine (or wherever you want to put it) declare a variable of the above type:
Code:
procedure MyProcedure
var
  MyVariableRecords: Array [1..10] of VariableRecord;
begin 
  ShowMessage(MyVariableRecords[1].Variable);
  ShowMessage(MyVariableRecords[1].VariableValue);
end;

Hope this helps
Clive [infinity]
 
Thanks for the suggestion Clive,

I will try what you say, but to illustrate further my problem:


My procedure is implemented as follows:
ssShowReport(sReportFile : string; aReportDataSet : TADOQuery;
arVariables, arValues : Array of string);
begin

{my code for whatever i want to be done}

end;

I call the procedure as follows:
ssShowReport('Cylinder_R', Cylinders,
['tCylinderStatus', 'tCylinderType', 'tCustomer'],
[cbStatus.Text, cbType.Text, cbSelector.Text]);


in another case i call the same procedure as follows
ssShowReport('Customer_R', dmCTracker.Customers,
['tCustomerT'], [cbSelector.Text]);

(The size of the arrays varies depending on the report being created.)

You notice that when i call the procedure i pass 2 arrays, i would like to write it in such a way that i pass only ONE array with pairs of 'Variable' and 'Value'

How would i do that?

TIA

cpmasesa
 
program StrPair;
{$APPTYPE CONSOLE}

type
tStringPair = Array[0..1] of string;


procedure SetVariableValues(aVar, aVal:string);
begin
WriteLn('VAR:', aVar, '; VAL:', aVal);
end;

procedure ssShowReport(StringPairs:array of tStringPair);
var
i:integer;
begin
for i:=low(StringPairs) to high(StringPairs) do
SetVariableValues( StringPairs[ i ][0], StringPairs[ i ][1]);
end;


begin
WriteLn('EXAMPLE ONE:');
ssShowReport( [ [ 'tCustomerT', 'cbSelector.Text'] ] );

WriteLn('EXAMPLE TWO:');
ssShowReport([
[ 'tCylinderStatus', 'cbStatus.Text' ],
[ 'tCylinderType', 'cbType.Text' ],
[ 'tCustomer', 'cbSelector.Text']
]);

end.
 
ppc386,

i tried your code but it refuses to compile.

it fails on:

ssShowReport( [ [ 'tCustomerT', 'cbSelector.Text'] ] );

(ie when you call the procedure) with error message 'Ordinal type required'

Am trying to figure out why it does that, if you do so before i post a solution, please let me know

cpmasesa
 
{
OOPS! You're right ...
( It works in Free Pascal but not Delphi, sorry )

Here is another idea:
}
program StrPair;
{$APPTYPE CONSOLE}


procedure SetVariableValues(aVar, aVal:string);
begin
WriteLn('VAR:', aVar, '; VAL:', aVal);
end;

procedure ssShowReport(Pairs:array of string);
var
i:integer;
begin
for i:=0 to high(Pairs) div 2 do
SetVariableValues( Pairs[ i * 2 ], Pairs[ i * 2 + 1 ]);
end;


begin
WriteLn('EXAMPLE ONE:');
ssShowReport([
'tCustomerT', 'cbSelector.Text'
]);

WriteLn('EXAMPLE TWO:');
ssShowReport([
'tCylinderStatus', 'cbStatus.Text',
'tCylinderType', 'cbType.Text',
'tCustomer', 'cbSelector.Text'
]);

end.
 
You might try the following approach:
Code:
procedure SomeProc(myArrOfParams: Variable);
var 
  i : Integer;
begin
// assuming that length of your arr will always be even 
//(any number of pairs of VarName/Value
  for i := 0 to VarArrayHighBound(myArrOfParams) do
   SetVariableValues(String(myArrOfParams[i]){ParamName},
   String(myArrOfParams[i + 1]){ParamValue});
end;
and a call to this proc would look like:
Code:
begin
...
  SomeProc(VarArrayOf(['ParamName1', 'ParamValue1', 'ParamName2', 'ParamValue2']));
// any number of pairs of 'ParamName', 'ParamValue'
...
end
HTH

--- markus
 
for statement should be like this of course:
for i := 0 to VarArrayHighBound(myArrOfParams) - 1 do

--- markus
 
Thanks guys, i finally have a working solution.

ppc386: i tested it and it works (after slight modifications to suit my excact needs) thanks

McMerfy: you solution is very similar to that of ppc386 BUT i think we have to use i * 2 for index of ParamName and i * 2 + 1 for index of paranvalue as suggested by ppc386 otherwise we would get inconsistent results. (Test it for i = 1 and see what we get)

Again, thanks

cpmasesa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top