Search code examples
pascallazarus

pascal adding tedit text to record


I am having problems with adding text I have entered into a tedit, into an record. Here is the code i currently have:

procedure TForm7.AddNewQuestionClick(Sender: TObject);
 var
   w: integer;
   QuestDesc, QuestAnsr: string;

 begin
   NewQuestID.text:=(GetNextQuestionID); //Increments QuestionID part of record
   w:=Length(TQuestions);  
   SetLength(TQuestions, w+1);
   QuestDesc:= NewQuestDesc.text;
   QuestAnsr:= NewQuestAns.text;
   TQuestionArray[w+1].Question:= QuestDesc; // Error on this line (No default property available)
   TQuestionArray[w+1].Answer:= QuestAnsr;

 end;

Here is the record I am trying to add to:

 TQuestion = record
  public
    QuestionID: integer;
    Question: shortstring;
    Answer: shortstring;
    procedure InitQuestion(anID:integer; aQ, anA:shortstring);
 end;  

TQuestionArray = array of TQuestion;

Any help solving this problem would be greatly appreciated.


Solution

  • You're missing a few things. You've declared a procedure to help initialize a new question - you should be using it.

    This should get you going:

    type
      TQuestion = record
        QuestionID: integer;
        Question: ShortString;
        Answer: ShortString;
        procedure InitQuestion(anID: Integer; aQ, aAns: ShortString);
      end;
    
      TQuestionArray = array of TQuestion;
    var
      Form3: TForm3;
    
    var
      Questions: TQuestionArray;
    
    procedure TForm7.AddNewQuestionClick(Sender: TObject);
    begin
      SetLength(Questions, Length(Questions) + 1);
      Questions[High(Questions)].InitQuestion(GetNextQuestionID, 
                                                NewQuestDesc.Text,
                                                NewQuestAns.Text);
    end;
    

    If you really want to do it individually setting the fields:

    procedure TForm7.AddNewQuestionClick(Sender: TObject);
    var
      Idx: Integer;
    begin
      SetLength(Questions, Length(Questions) + 1);
      Idx := High(Questions);
      Questions[Idx].QuestionID := GetNextQuestionID;
      Questions[Idx].Question := NewQuestDesc.Text;
      Questions[Idx].Answer := NewQuestAns.Text;
    end;