Recent

Author Topic: Error: Identifier not found "Edit1" Why  (Read 5429 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Error: Identifier not found "Edit1" Why
« on: January 26, 2018, 10:13:23 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.     Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.     { TForm1 }
  13.  
  14.     TForm1 = class(TForm)
  15.         Button1: TButton;
  16.         Button2: TButton;
  17.         Edit1: TEdit;
  18.         Edit2: TEdit;
  19.         Label1: TLabel;
  20.         Label2: TLabel;
  21.         procedure Button1Click(Sender: TObject);
  22.         procedure Button2Click(Sender: TObject);
  23.  
  24.  
  25.     private
  26.  
  27.     public
  28.  
  29.     end;
  30.  Const
  31.      C_FNAME = 'textfile.txt';
  32.  
  33. var
  34.     Form1: TForm1;
  35.     tfOut: TextFile;
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. Procedure CreateFile;
  41. Begin
  42.   // Set the name of the file that will be created
  43.   AssignFile(tfOut, C_FNAME);
  44.  
  45.   {$I+}
  46.  
  47.  
  48.  try
  49.     // Create the file, write some text and close it.
  50.     rewrite(tfOut);
  51.     writeln(tfOut, 'Line 1');
  52.     writeln(tfOut, 'Line 2');
  53.     CloseFile(tfOut);
  54.  
  55.   except
  56.     // If there was an error the reason can be found here
  57.     on E: EInOutError do
  58.          showmessage('Error opening file = '+inttostr(ioresult));
  59.     end;
  60.     Edit1.Text := C_Fname;
  61.     Edit2.text :=  inttostr(ioresult)
  62.    end;
  63.  
  64.  
  65.  
  66. //end;
  67.  
  68. { TForm1 }
  69.  
  70.  
  71. procedure TForm1.Button1Click(Sender: TObject);
  72. begin
  73.       CreateFile;
  74. end;
  75.  
  76. procedure TForm1.Button2Click(Sender: TObject);
  77. begin
  78.     Close;
  79. end;
  80.  
  81. end.
  82.  

So I don't understand why Edit1 and Edit2 not found.

//Compile Project, Target: project1.exe: Exit code 1, Errors: 2
//unit1.pas(60,5) Error: Identifier not found "Edit1"
//unit1.pas(61,5) Error: Identifier not found "Edit2"


Have to write Form1.Edit1.text =
Have to write Form1.Edit1.text =
« Last Edit: January 26, 2018, 10:41:11 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Error: Identifier not found "Edit1" Why
« Reply #1 on: January 26, 2018, 10:34:15 pm »
Edit1 and Edit2 exist within the context of the class TForm1. Several instances of this class can exist. Therefore you must always specify the instance to which Edit1 and Edit2 belong. Since your class is probably instantiated as Form1 you must mention Form1.Edit1 and Form1.Edit2.

But this is not the recommended way because now your procedure "CreateFile" will only work if your form is named as "Form1". It is much better to make CreateFile a "method" of the class TForm1, i.e. you declare it within TForm1. The form itself "knows" that Edit1 and Edit2 belong to it, and therefore there is not need to specify the form name here. In addition, you should also declare tfOut as a variable of the class because you probably won't need it from outside the form:

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.   ,,,
  4.   private  
  5.     tfOut: TextFile;  // or put these in the "public" section if you call these from somwhere outside TForm1
  6.     procedure CreateFile;  
  7.    ....
  8.   end;
  9.  
  10. procedure TForm1.CreateFile;
  11. Begin
  12.   // Set the name of the file that will be created
  13.   AssignFile(tfOut, C_FNAME);
  14.   ....
  15.   Edit1.Text := C_Fname;
  16.   Edit2.Text := IntToStr(IOResult);
  17. end;

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Error: Identifier not found "Edit1" Why (Solved) - Reopened
« Reply #2 on: January 26, 2018, 10:40:46 pm »
So I declared the procedure in Private and I get:

unit1.pas(26,17) Error: Forward declaration not solved "CreateFile;"
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Error: Identifier not found "Edit1" Why (Solved) - Reopened
« Reply #3 on: January 26, 2018, 10:52:10 pm »
So I declared the procedure in Private and I get:

unit1.pas(26,17) Error: Forward declaration not solved "CreateFile;"
You forgot to put TForm1. before CreateFile in the implementation part.
Like wp showed you in line 10 of his snippet:
procedure TForm1.CreateFile;

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Error: Identifier not found "Edit1" Why
« Reply #4 on: January 26, 2018, 11:09:40 pm »
So I declare it in the private and add the class name TForm1.CreateFile where I write the procedure.

If I want it global to the program do I declare it in Public.

Would I still write:

Procedure TForm1.CreateFile;
Begin
.
.
.
End;

 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Error: Identifier not found "Edit1" Why
« Reply #5 on: January 26, 2018, 11:13:35 pm »
So I declare it in the private and add the class name TForm1.CreateFile where I write the procedure.
If I want it global to the program do I declare it in Public.
Would I still write:
Procedure TForm1.CreateFile;
Yes, your implementation part won't change and you keep Procedure TForm1.CreateFile;
You only move the declaration-line to the public part.

In other parts of your program (other forms) you can then call Form1.CreateFile.
If it is in the private part you can only call CreateFile from a procedure which is declared in TForm1 itself (which is usually enough).

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Error: Identifier not found "Edit1" Why
« Reply #6 on: January 27, 2018, 12:46:42 am »
I think I kinds understand.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018