Stretchwickster
Programmer
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:
It is defined as follows:
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.
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.
[/li]
[li]Finally, I called the BuildAsciiArray function as part of the Format function call.
LOCATION was a constant defined as:
[/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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"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
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;
[/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]Finally, I called the BuildAsciiArray function as part of the Format function call.
Code:
desc := Format(LOCATION, BuildAsciiArray(20, Ed_Description.Text));
Code:
LOCATION = 'Location,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d';
[/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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"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