Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 981
user477276 Avatar asked Apr 16 '12 15:04

user477276


People also ask

Can we declare array as const?

Arrays are Not Constants The keyword const is a little misleading. It does NOT define a constant array. It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.

Can array declared as const in JavaScript?

To create a const array in JavaScript we need to write const before the array name. The individual array elements can be reassigned but not the whole array.

How do you declare a constant array in C++?

In C++, the most common way to define a constant array should certainly be to, erm, define a constant array: const int my_array[] = {5, 6, 7, 8};

How do you define array in #define?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it. int data[100];


1 Answers

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;
like image 57
delianmc Avatar answered Sep 22 '22 14:09

delianmc