Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create functions that can accept variable number of parameters such as Fo Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
24-Jan-03
Category
Algorithm
Language
Delphi 2.x
Views
147
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler

How to create functions that can accept variable number of parameters such as 
Format()

Answer:

Sometimes it's necessary to pass undefined number of [different type] variables to 
a function -- look at Format() function in Delphi and *printf() functions in  C/C++ 
for example. Once you analyze the following code, you'll be on your way to creating 
mysterious variable parameter functions... 
1   
2   // FunctionWithVarArgs()
3   //
4   // skeleton for a function that
5   // can accept vairable number of
6   // multi-type variables
7   //
8   // here are some examples on how
9   // to call this function:
10  //
11  // FunctionWithVarArgs(
12  //   [ 1, True, 3, '5', '0' ] );
13  //
14  // FunctionWithVarArgs(
15  //   [ 'one', 5 ] );
16  //
17  // FunctionWithVarArgs( [] );
18  //
19  
20  procedure FunctionWithVarArgs(
21    const ArgsList: array of const);
22  var
23    ArgsListTyped:
24    array[0..$FFF0 div SizeOf(TVarRec)]
25      of TVarRec absolute ArgsList;
26    n: integer;
27  begin
28    for n := Low(ArgsList) to
29      High(ArgsList) do
30    begin
31      with ArgsListTyped[n] do
32      begin
33        case VType of
34          vtInteger:
35            begin
36              {handle VInteger here}
37            end;
38          vtBoolean:
39            begin
40              {handle VBoolean here}
41            end;
42          vtChar:
43            begin
44              {handle VChar here}
45            end;
46          vtExtended:
47            begin
48              {handle VExtended here}
49            end;
50          vtString:
51            begin
52              {handle VString here}
53            end;
54          vtPointer:
55            begin
56              {handle VPointer here}
57            end;
58          vtPChar:
59            begin
60              {handle VPChar here}
61            end;
62          vtObject:
63            begin
64              {handle VObject here}
65            end;
66          vtClass:
67            begin
68              {handle VClass here}
69            end;
70          vtWideChar:
71            begin
72              {handle VWideChar here}
73            end;
74          vtPWideChar:
75            begin
76              {handle VPWideChar here}
77            end;
78          vtAnsiString:
79            begin
80              {handle VAnsiString here}
81            end;
82          vtCurrency:
83            begin
84              {handle VCurrency here}
85            end;
86          vtVariant:
87            begin
88              {handle VVariant here}
89            end;
90        else
91          begin
92            {handle unknown type here}
93          end;
94        end;
95      end;
96    end;
97  end;
98  
99  //
100 // example function created using
101 // the above skeleton
102 //
103 // AddNumbers() will return the
104 // sum of all the integers passed
105 // to it
106 //
107 // AddNumbers( [1, 2, 3] )
108 //   will return 6
109 //
110 //
111 
112 function AddNumbers(
113   const ArgsList: array of const)
114   : integer;
115 var
116   ArgsListTyped:
117   array[0..$FFF0 div SizeOf(TVarRec)]
118     of TVarRec absolute ArgsList;
119   n: integer;
120 begin
121   Result := 0;
122   for n := Low(ArgsList) to
123     High(ArgsList) do
124   begin
125     with ArgsListTyped[n] do
126     begin
127       case VType of
128         vtInteger: Result := Result + VInteger;
129       end;
130     end;
131   end;
132 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC