'How to define an array in const?
I'm having some problems defining an array of strings in const under the code section in Inno Setup, I have the following:
[Code]
const
listvar: array [0..4] of string =
('one', 'two', 'three', 'four', 'five');
It's saying I need an = where the : is, but then I can't define it as an array.
Solution 1:[1]
I made a little utility function a little while ago. It won't allow you to assign an array on a constant but it could do the trick for a variable in a one liner. Hoping this help.
You can use it this way:
listvar := Split('one,two,three,four,five', ',');
{ ============================================================================ }
{ Split() }
{ ---------------------------------------------------------------------------- }
{ Split a string into an array using passed delimeter. }
{ ============================================================================ }
Function Split(Expression: String; Separator: String): TArrayOfString;
Var
i, p : Integer;
tmpArray : TArrayOfString;
curString : String;
Begin
i := 0;
curString := Expression;
Repeat
SetArrayLength(tmpArray, i+1);
If Pos(Separator,curString) > 0 Then
Begin
p := Pos(Separator, curString);
tmpArray[i] := Copy(curString, 1, p - 1);
curString := Copy(curString, p + Length(Separator), Length(curString));
i := i + 1;
End Else Begin
tmpArray[i] := curString;
curString := '';
End;
Until Length(curString)=0;
Result:= tmpArray;
End;
Solution 2:[2]
I am using the following solution. It is especially well suited for initializing large arrays (for example, CRC tables) because it preserves the original syntax for data.
var
listvar: array of string;
procedure fill_listvar(var arr: array of string; const data: array of string);
var
i: integer;
begin
SetArrayLength(arr, GetArrayLength(data));
for i := 0 to GetArrayLength(data) - 1 do
arr[i] := data[i];
end;
procedure Init();
begin
fill_listvar(listvar, ['one', 'two', 'three', 'four', 'five']);
end;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Martin Prikryl |
| Solution 2 | Martin Prikryl |
