SenorGeraldo
SenorGeraldo

Reputation: 49

Hi, I keep getting an access violation while setting the length of my dynamic arrays

procedure TfrmMain.createpnl(i: integer);
var
  j,c: integer;
begin
  c:=5;
  top := pnlResult1.top;
  for j := 1 TO i do
    if (arrFound[j] <> -1) or (arrFound[j] <> 0) then
    begin
      with dmAll do
      begin
        tblHouses.First;
        while not tblHouses.Eof do
        begin
          if tblHouses['ID'] = arrFound[j] then
          begin
            if j > 1 then
              itop := j * pnlResult1.top + (j - 1) * pnlResult1.Height;
            SetLength(arrpnl, c);
            SetLength(arrimg, c);
            SetLength(arrlbl[1], c);
            SetLength(arrlbl[2], c);
            SetLength(arrlbl[3], c);
            SetLength(arrlbl[4], c);
            SetLength(arrlbl[5], c);
            SetLength(arrlbl[6], c);

            { the violation usually happes at arrlbl[6] but it has been in the neighboring area before }

            /// ///////////dupe panels
            arrpnl[c] := TPanel.Create(frmMain);
            with arrpnl[c] do
            begin
              Parent := scbMain;
              Width := pnlResult1.Width;
              Height := pnlResult1.Height;
              left := pnlResult1.left;
              top := itop;
              Visible := true;
              Color := pnlResult1.Color;
            end;
            frmMain.Position:=poScreenCenter;
            /// //////////dupe photos
            arrimg[c] := TImage.Create(frmMain);
            with arrimg[c] do
            begin
              Parent := arrpnl[c];
              Width := Image1.Width;
              Height := Image1.Height;
              left := Image1.left;
              top := Image1.top;
            end;

            { i cut some spaghetti code to shorten question }

            tblPhotos.First;
            while NOT tblPhotos.Eof do
            begin
              if tblPhotos['HouseID'] = tblHouses['ID'] then
                if fileexists(tblPhotos['photo']) then
                begin
                  arrimg[c].Picture.LoadFromFile(tblPhotos['photo']);
                  arrimg[c].Stretch := true;
                end
                else
                begin
                  if fileexists('H:\v0.1\not-found-image-15383864787lu.jpg') then
                  begin
                    arrimg[c].Picture.LoadFromFile
                      ('H:\v0.1\not-found-image-15383864787lu.jpg');
                    arrimg[c].Stretch := true;
                  end;
                end;
              tblPhotos.Next
            end;
            tblOwners.First;
            while NOT tblOwners.Eof do
            begin
              if tblOwners['ID'] = tblHouses['hOwner'] then
              begin
                arrlbl[4][c].caption := 'Email: ' + tblOwners['oEmail'] + #10 +
                  'Cell number: ' + tblOwners['oCell'];
              end;
              tblOwners.Next;
            end;
            inc(c);
            bFound := true;
          end;
          tblHouses.Next;
        end;
      end;
    end;
end;

I search through a database to find guesthouses that match the search criteria in an above procedure.

The search returns an array (arrFound) filled with ID's of houses that match search criteria.

I then make all duplicate results -1 and create TPanels dynamically to return the guesthouses as browseable results in a TScrollBox.

The dynamic array in question (arrlbl) is a 2D array of TLabels with a predetermined first value and a dynamic second value (depending on the amount of results).

I used 1D arrays but that gave the same error and I have a similar procedure on another form that doesn't give any errors.

Upvotes: 1

Views: 186

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

It seems like you got the indices wrong.

In general, a dynamic array with n elements has indices 0..n - 1.

For example, if a is a dynamic array with 5 elements, the elements are a[0], a[1], a[2], a[3], and a[4]. There is no a[5].

Upvotes: 2

Related Questions