Wes Wakeman
Wes Wakeman

Reputation: 31

An array of records inside and array of records in Pascal

I'm having problems creating an array of records with an array of records inside.

type
    SubjectsRec = array of record
        subjectName : String;
        grade : String;
        effort : Integer;
    end;
    TFileRec = array of record
        examinee : String;
        theirSubjects: array of SubjectsRec;
    end;

var
    tfRec: TFileRec;
    i: Integer;
begin
    setLength(tfRec,10);
    for i:= 0 to 9 do
    begin
       setLength(tfRec[i].theirSubjects,10);
    end;

After this I was hoping to assign values by doing this:

tfRec[0].theirSubjects[0].subjectName:= "Mathematics";

However, I get:

Error: Illegal qualifier

when trying to compile.

Upvotes: 1

Views: 3862

Answers (2)

Enoh Retsev
Enoh Retsev

Reputation: 11

I think you should change this:

tfRec[0].theirSubjects[0].subjectName:= "Mathematics";

to

tfRec[0].theirSubjects[0].subjectName:= 'Mathematics';

Upvotes: 1

brandizzi
brandizzi

Reputation: 27050

You declared SubjectsRec as an array of records, and then declared the TheirSubjects field as an array of SubjectRecs - that is, TheirSubjects is an array of arrays of records.

There are two solutions:

Declare TheirSubjects as having the type SubjectsRec, instead of array of subjectsRec:

TFileRec = array of record
  examinee : string;
  theirSubjects: SubjectsRec;
end;

or declare SubjectsRec as a record, not as an array. This is my favorite one:

SubjectsRec = record
  subjectName : String;
  grade : String;
  effort : Integer;
end;
TFileRec = array of record
  examinee : string;
  theirSubjects: array of SubjectsRec;
end;

Also, strings in Pascal are delimited by single quotes, so you should replace "Mathematics" by 'Mathematics'.

Upvotes: 2

Related Questions