Search code examples
delphidelphi-xe6

Delphi giving syntax errors: . expected but ; found error


I am toying around in Delphi XE6, and add this procedure on a button:

procedure TTabbedForm.btnLoadingClick(Sender: TObject);
var
  dlg: Unit2;

begin

  dlg := Form2.Create(nil);
  Form2.ShowModal();

end;

Delphi gives me the following error while compiling:

[DCC Error] TabbedTemplate.pas(53): E2029 '.' expected but ';' found
[DCC Error] TabbedTemplate.pas(55): E2029 ';' expected but 'BEGIN' found
[DCC Fatal Error] Speelpleintjes.dpr(7): F2063 Could not compile used unit 'TabbedTemplate.pas'

Line 53 being: dlg: Unit2;

Honestly i'm quite puzzled, the syntax seems correct, the procedure is auto generated from the events tab.

Any suggestions?


Solution

  • If Unit2 is another unit in your project (judging by it's name it probably is), it can't be used as a type directly. Units only declare things. That's why the compiler is expecting a . since you can prefix an identifier with the unit's name to direct to a declaration in that specific unit.

    To correct this, write dlg: TForm2;

    Also it's common practice to call constructors from the class declaration, not a variable so change it to:

    dlg := TForm2.Create(nil);
    

    Or use the Form2 variable, which typically will hold an instance of TForm2 already. By default extra forms added to a Delphi project are created hidden, ready for Show or ShowModal.