I am currently writing Pascal Script code for usage in Inno Setup.
Inno provides a function called
FmtMessage to format a string. It is defined as
function FmtMessage(const S: String; const Args: array of String): String;, see also Inno Setup Help (jrsoftware.org).
Now I have an issue calling this method.
When I call it normally like that:
FmtMessage('Some %1 test %2 string %3', [IntToStr(100), 'Somevalue', IntToStr(5)]); it works.
However I wanted to wrap that method in a helper function, because the first string comes from the translation system, and pass the arguments (that Array of String) through that method. When I do that, I get a Type Mismatch runtime error:
function FormatCustomMessage(const messageId: String; const arguments: Array of String): String; begin result := FmtMessage(CustomMessage(messageId), arguments); end;
I wasn’t sure why that was, but I figured out that the problem is the arguments part.
When I prepare the arguments differently, I also get the Type Mismatch runtime error:
function FormatCustomMessage(): String; var message: String; args: Array of String; begin message := 'Some %1 test %2 string %3'; args := [IntToStr(100), 'Caption', IntToStr(100)]; result := FmtMessage(message, args); // Type Mismatch error end;
So the only difference here is, that I assign the Array to a variable and pass that, instead of writing it directly into the
FmtMessage call. And that results in a Type Mismatch error, and I don’t understand why.
I also already tried this:
function FormatCustomMessage(): String; var message: String; args: Array of String; begin message := 'Some %1 test %2 string %3'; SetArrayLength(args, 3); args := IntToStr(100); args := 'Caption'; args := IntToStr(5); result := FmtMessage(message, args); // Type Mismatch error end;
Same thing. If I write the
['param1', 'param2'] array in the call directly it works, when passing it from a variable with the same type, it doesn’t.
I didn’t really touch pascal for the last 20 years or so, however I have a Delphi background, and not being capable of passing an Array of String through one function to the next makes me feel a bit dumb.
Am I doing something wrong here, or is that a problem with Pascal Script?
I mean, I don’t really need that helper function, as its an installer script and I can put the nested call to FmtMessage() with CustomText() everywhere, but I really like to know what I did wrong here, if that’s the case.