Recent

Author Topic: [SOLVED] Identifier not found - TEdit  (Read 1870 times)

nick51

  • Newbie
  • Posts: 5
[SOLVED] Identifier not found - TEdit
« on: November 23, 2020, 09:49:57 am »
I used TurboPascal many years ago, and am now trying to use Lazarus to write a GUI program.

I have created a form TfrmTemp.
On the form, I have included a TEdit control named 'edtSepChar'.
In the body of the code, I have defined a a global variable 'sepChar'.
I want to assign the value of 'edtSepChar' to that variable, using 'edtSepChar.Text'.
When I try to compile, I get message 'Error: Identifier not found "edtSepChar"'

I have tried including the assignment in a procedure and also in the main program, but keep getting the same error.

This is part of my code (I have also attached the full Project/Publish zip file):
Code: Pascal  [Select][+][-]
  1. type
  2.   { TfrmTemp }
  3.   TfrmTemp = class(TForm)
  4.     edtSepChar: TEdit;
  5.     lblSepChar: TLabel;
  6. var
  7.   frmTemp: TfrmTemp;
  8.   sepChar: string;     //global variable
  9.  
  10. { TfrmTemp }
  11.  
  12. procedure PROC1;
  13. begin
  14.   sepChar:='PROC1';
  15.   ShowMessage('Value in Proc1 is:   ' + sepChar);   // This works
  16. end;
  17.  
  18. procedure PROC2;
  19. begin
  20.   sepChar := edtSepChar.Text;   // This statement causes error message 'Identifier not found'
  21.   ShowMessage('Value in Proc2 is:   ' + sepChar);
  22. end;
  23.  
  24. //MAIN PROGRAM
  25. begin
  26.   sepChar := edtSepChar.Text;   // This statement causes error message 'Identifier not found'
  27.   ShowMessage('Separator in Main Program is:   ' + sepChar);
  28.   PROC1;
  29.   PROC2
  30. end.
  31.  

I will be very grateful if you can tell me what I am doing wrong, and explain what needs to be changed.

Windows 7 Home Premium SP1 / FPC v3.2.0 / Lazarus IDE v2.0.10 r63526
Code: Pascal  [Select][+][-]
  1.  

« Last Edit: November 23, 2020, 01:49:06 pm by nick51 »

abunakov

  • Newbie
  • Posts: 3
Re: Identifier not found - TEdit
« Reply #1 on: November 23, 2020, 10:15:56 am »
Replace all occurences of edtSepChar.Text with frmTemp.edtSepChar.Text in your methods and main program block.

edtSepChar is a member of TfrmTemp class, so it must be accessed via object instance variable frmTemp.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Identifier not found - TEdit
« Reply #2 on: November 23, 2020, 10:44:22 am »
The section in your tempunit headed //MAIN PROGRAM is in error.
The structure of a unit differs from that of the main program (temp.lpr in your case).
A unit's implementation section should not have a
Code: Pascal  [Select][+][-]
  1. begin...end.
code block; whereas the main program must have such a block.
The routine(s) you hoped would be executed in your "main program" block need to be placed in a method of the form.
For instance, to have them executed at the form's initialization, double-click on the form in the designer to create an OnCreate handler, and complete it as follows:
Code: Pascal  [Select][+][-]
  1. unit tempunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Dialogs, StdCtrls, Classes;
  9.  
  10. type
  11.   { TfrmTemp }
  12.   TfrmTemp = class(TForm)
  13.     edtSepChar: TEdit;
  14.     lblSepChar: TLabel;
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     procedure Proc1;
  18.     procedure Proc2;
  19.   public
  20.   end;
  21.  
  22. var
  23.   frmTemp: TfrmTemp;
  24.   sepChar: string;     //global variable
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TfrmTemp }
  31.  
  32. procedure TfrmTemp.FormCreate(Sender: TObject);
  33. begin
  34.   sepChar := 'MAIN';
  35.   sepChar := edtSepChar.Text;
  36.   ShowMessage('Separator in Main Program is:   ' + sepChar);
  37.   PROC1;
  38.   PROC2;
  39. end;
  40.  
  41. procedure TfrmTemp.Proc1;
  42. begin
  43.   sepChar:='PROC1';
  44.   ShowMessage('Value in Proc1 is:   ' + sepChar);
  45. end;
  46.  
  47. procedure TfrmTemp.Proc2;
  48. begin
  49.   sepChar:='PROC2';
  50.   sepChar := edtSepChar.Text;
  51.   ShowMessage('Value in Proc2 is:   ' + sepChar);
  52. end;
  53.  
  54. {//MAIN PROGRAM
  55. begin
  56.   sepChar := 'MAIN';
  57. //  sepChar := edtSepChar.Text;   // This statement causes error message 'Identifier not found'
  58.   ShowMessage('Separator in Main Program is:   ' + sepChar);
  59.   PROC1;
  60.   PROC2 }
  61. end.
Making Proc1 and Proc2 form methods (rather than independent routines unrelated to TfrmTemp) removes the "Identifier not found" error message.



nick51

  • Newbie
  • Posts: 5
Re: Identifier not found - TEdit
« Reply #3 on: November 23, 2020, 11:51:17 am »
Replace all occurences of edtSepChar.Text with frmTemp.edtSepChar.Text in your methods and main program block.

edtSepChar is a member of TfrmTemp class, so it must be accessed via object instance variable frmTemp.

Thank you for replying so quickly. I added the prefix 'frmTemp.' as suggested. The program now compiles successfully, but produces a SIGSEGV error at runtime. See attachment. Looks like there must be something else that I am doing wrong.

dseligo

  • Hero Member
  • *****
  • Posts: 1406
Re: Identifier not found - TEdit
« Reply #4 on: November 23, 2020, 12:21:14 pm »
Read howardpc's answer again.
Problem is that you are accessing form before it is created.

nick51

  • Newbie
  • Posts: 5
Re: Identifier not found - TEdit
« Reply #5 on: November 23, 2020, 12:32:28 pm »
The section in your tempunit headed //MAIN PROGRAM is in error.
The structure of a unit differs from that of the main program (temp.lpr in your case).
A unit's implementation section should not have a
Code: Pascal  [Select][+][-]
  1. begin...end.
code block; whereas the main program must have such a block.
The routine(s) you hoped would be executed in your "main program" block need to be placed in a method of the form.
For instance, to have them executed at the form's initialization, double-click on the form in the designer to create an OnCreate handler, and complete it as follows:
Code: Pascal  [Select][+][-]
  1. unit tempunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Dialogs, StdCtrls, Classes;
  9.  
  10. type
  11.   { TfrmTemp }
  12.   TfrmTemp = class(TForm)
  13.     edtSepChar: TEdit;
  14.     lblSepChar: TLabel;
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     procedure Proc1;
  18.     procedure Proc2;
  19.   public
  20.   end;
  21.  
  22. var
  23.   frmTemp: TfrmTemp;
  24.   sepChar: string;     //global variable
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TfrmTemp }
  31.  
  32. procedure TfrmTemp.FormCreate(Sender: TObject);
  33. begin
  34.   sepChar := 'MAIN';
  35.   sepChar := edtSepChar.Text;
  36.   ShowMessage('Separator in Main Program is:   ' + sepChar);
  37.   PROC1;
  38.   PROC2;
  39. end;
  40.  
  41. procedure TfrmTemp.Proc1;
  42. begin
  43.   sepChar:='PROC1';
  44.   ShowMessage('Value in Proc1 is:   ' + sepChar);
  45. end;
  46.  
  47. procedure TfrmTemp.Proc2;
  48. begin
  49.   sepChar:='PROC2';
  50.   sepChar := edtSepChar.Text;
  51.   ShowMessage('Value in Proc2 is:   ' + sepChar);
  52. end;
  53.  
  54. {//MAIN PROGRAM
  55. begin
  56.   sepChar := 'MAIN';
  57. //  sepChar := edtSepChar.Text;   // This statement causes error message 'Identifier not found'
  58.   ShowMessage('Separator in Main Program is:   ' + sepChar);
  59.   PROC1;
  60.   PROC2 }
  61. end.
Making Proc1 and Proc2 form methods (rather than independent routines unrelated to TfrmTemp) removes the "Identifier not found" error message.

Thank you for posting reply so quickly. These changes achieve what I wanted. I will now do some homework to learn more about exactly WHY they work - there are clearly some (many?) gaps in my understanding of objects and also the function of the LPR file  :o.

I did notice, however, that when I execute this code the form is displayed and nothing else happens - ShowMessage is not executed, and neither are Proc1 and/or Proc2. The only way to continue is to close the form window, whereupon the program terminates. Is there something else that I need to do to make FormCreate work?

But I added a TButton with OnClick event set to execute Proc1 and Proc2; that runs those procs and they display the correct values. So your changes did allowed me to pass the values as required, so a big "thank you!"  :D
« Last Edit: November 23, 2020, 12:40:43 pm by nick51 »

bytebites

  • Hero Member
  • *****
  • Posts: 680
Re: Identifier not found - TEdit
« Reply #6 on: November 23, 2020, 12:41:30 pm »
You need to set the FormCreate event too.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Identifier not found - TEdit
« Reply #7 on: November 23, 2020, 12:50:04 pm »
I did notice, however, that when I execute this code the form is displayed and nothing else happens - ShowMessage is not executed, and neither are Proc1 and/or Proc2. The only way to continue is to close the form window, whereupon the program terminates. Is there something else that I need to do to make FormCreate work?

But I added a TButton with OnClick event set to execute Proc1 and Proc2; that runs those procs and they display the correct values. So your changes did allowed me to pass the values as required, so a big "thank you!"  :D
For a FormCreate() to execute it must be called!
Double-clicking the form in the designer to create an OnCreate does two things: it generates the skeleton for the method in the form's type definition and implementation, and it also connects the new method to the form's OnCreate method property.
It is this last step that has not happened. You can do it manually via the Object Inspector by clicking the Events page for frmTemp, and in the dropdown for OnCreate make sure the property value is set to FormCreate. Otherwise it will be set to (none), and so your handler is never called.

nick51

  • Newbie
  • Posts: 5
Re: Identifier not found - TEdit
« Reply #8 on: November 23, 2020, 01:46:53 pm »
I did notice, however, that when I execute this code the form is displayed and nothing else happens - ShowMessage is not executed, and neither are Proc1 and/or Proc2. The only way to continue is to close the form window, whereupon the program terminates. Is there something else that I need to do to make FormCreate work?

But I added a TButton with OnClick event set to execute Proc1 and Proc2; that runs those procs and they display the correct values. So your changes did allowed me to pass the values as required, so a big "thank you!"  :D
For a FormCreate() to execute it must be called!
Double-clicking the form in the designer to create an OnCreate does two things: it generates the skeleton for the method in the form's type definition and implementation, and it also connects the new method to the form's OnCreate method property.
It is this last step that has not happened. You can do it manually via the Object Inspector by clicking the Events page for frmTemp, and in the dropdown for OnCreate make sure the property value is set to FormCreate. Otherwise it will be set to (none), and so your handler is never called.

Many thanks for your patience and excellent explanation :D. Now for some serious homework - old TurboPascal habits die hard  :o. I will mark this thread as closed.

nick51

  • Newbie
  • Posts: 5
Re: Identifier not found - TEdit
« Reply #9 on: November 23, 2020, 01:50:54 pm »
You need to set the FormCreate event too.
Thank you. As replied to HeroMember, I have some homework!

 

TinyPortal © 2005-2018