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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass array to Format function 2

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I ran into problems whilst trying to pass an array as the second argument to the Format function (
For those of you not familiar with the Format function, it is an extremely useful function which:
Delphi Help File said:
returns a formatted string assembled from a format string and an array of arguments.

It is defined as follows:
Code:
function Format(const Format: string; const Args: array of const): string;

I wanted to be able to pass a whole array (from a function result), rather than pass individual array items, to the "Args" parameter. This proved tricky, so I just wanted to share with you how I managed this.

[ol][li]I declared an array type for TVarRec records.
Code:
TVarRecArr = array of TVarRec;
The TVarRec type is the type that Delphi internally uses when there's an "array of const" parameter in a function. The official term for such a parameter is a "variant open array parameter" which allows you to pass an array of differently-typed expressions to a single procedure or function. The Delphi compiler translates each array item to a TVarRec value.

[/li]
[li]I declared the function whose result would be used as the "Args" argument of the Format function, ensuring that its' result type was of type TVarRecArr. I dimensioned the result array by using SetLength. The important thing to note is that for each array item, its type needs to be set using the VType field (e.g. vtInteger - see the Delphi help file for all the different types) and the field corresponding to the VType you entered needs to be filled (e.g. in this case VType=vtInteger so the VInteger field needed to be populated with a value).

Incidentally, I needed a function which, when passed a string, would create an array of ASCII codes with each code corresponding to a character in the original string. The parameter ArrayLength was used to specify the maximum size of my array and any unused items were automatically assigned a value of zero. So if the ArrayLength was 50 and AString was only 10 characters in length, the returned array would contain 50 items, the first 10 representing characters of AString and the remaining 40 containing zeros.
Code:
  // return type is TVarRecArr so that array will be accepted by Format function
  function BuildAsciiArray(ArrayLength: Integer; AString: String): TVarRecArr;
  var
    i: Integer;
  begin
    SetLength(Result, ArrayLength);
    for i := 0 to Length(AString) - 1 do
    begin
      // need to set type to Integer as a variant array
      // doesn't naturally know the type of an array item
      Result[i].VType := vtInteger;
      // insert the ASCII value into the variant array
      Result[i].VInteger := Ord(AString[i + 1]);
    end;
  end;
[/li]
[li]Finally, I called the BuildAsciiArray function as part of the Format function call.
Code:
desc := Format(LOCATION, BuildAsciiArray(20, Ed_Description.Text));
LOCATION was a constant defined as:
Code:
LOCATION = 'Location,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d';
[/li]
[/ol]

Hopefully someone will find this useful as I struggled a bit to collate all the information I needed from the Delphi help alone.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I struggled with this a long time ago.
anyway, star for you, maybe turn this into a FAQ?

//Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Good idea, whosrdaddy, I'll add an FAQ when I get some free time - at this rate it might be 2008!

The main reason for posting it in the forum was because no results were returned when I searched for TVarRec in this forum before posting. So, hopefully people will come across this thread once the page has been indexed.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Well, tek-tips search feature needs fixing then :) see thread102-981781 for what we discussed 2 years ago.
 
lol, didn't remember that one!

[atom]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
As another contributor to that thread - I didn't remember either! It was 2 years ago, so I think we're allowed a memory lapse on that one. Apologies all, the Forum Search didn't bring back any results on TVarRec! I just tried a google search for "site: tvarrec" and that only returned this new thread!

Well done Griffyn for remembering. Interestingly, last time round I suggested an alternative approach rather than overcoming the problem, so I feel like I've achieved something cos I hadn't seen (or remembered seeing) whosrdaddy's solution.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top