Delphi 2010: Run time error 217 at 00406786.

I don’t know what the error is, here’s the code:

procedure TForm1.FormCreate(Sender: TObject); var Params:TStringList; InetParams:TStringList; i,k:Integer; inipath:string; begin {Settings section start} inipath:=GetCurrentDir+'\services.ini'; Settings:=TIniFile.Create(inipath); Params:= TStringList.Create; InetParams:=TStringList.Create; try Params.StrictDelimiter:= True; Params.CommaText:= Settings.ReadString('Telephone', 'catidx_values', '-1'); TEL_SERV_COUNT:=Params.Count; SetLength(tel_serv, TEL_SERV_COUNT); i:=1;k:=1; while i<=TEL_SERV_COUNT do begin tel_serv[I] := Trim(Params[I-1]); inc(i); end; InetParams.CommaText:= Settings.ReadString('Internet', 'catidx_values', '-1'); INET_SERV_COUNT:=InetParams.Count; SetLength(inet_serv, INET_SERV_COUNT); while k<=INET_SERV_COUNT do begin inet_serv[k]:= Trim(InetParams[k-1]); //ShowMessage(Inet_serv[i]); // ShowMessage(InetParams[k-1]); Inc(k); end; finally Params.Free; InetParams.Free; Settings.Free; end; {Settings section end} end; 

 procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin //Application.Terminate; end; 

I have already tried all the options for closing the program, but all the time it shows an early-time error. Why? PS: It is interesting that if you comment the line in the second cycle

 inet_serv[k]:= Trim(InetParams[k-1]); 

then there will be no mistake at all ... What could be, pliz ... Waiting for help and advice.

  • Strange, removed the SetLength code, and set the length of the arrays manually, and it began to work properly .. - Nurbol Mambetov

2 answers 2

As I understood from the code, tel_serv and inet_serv are arrays of type array of string. if so, then in the code the access to the elements of these arrays occurs at the wrong indexes. in dynamic arrays, zero-based indexing is used; in the code, 1-based is used. it is enough to replace tel_serv [I] with tel_serv [I - 1] and inet_serv [k] with inet_serv [k - 1] and the error will disappear.

    Yes, @maxionans is right, you have wrong references to the elements of the dynamic array.
    Read the section on lists, dynamic arrays and how SetLength works.
    In your code, you would need something like this:

     ... i := 0; while i < TEL_SERV_COUNT do begin tel_serv[i] := Trim(Params[i]); inc(i); end; ... k := 0; while k < INET_SERV_COUNT do begin inet_serv[k]:= Trim(InetParams[k]); Inc(k); end; ... 
    • Thank you, everyone, for the answers. I will try as you said. - Nurbol Mambetov