Why does the compiler say “Undeclared identifier” for my form's fields?

前端 未结 2 1310
不思量自难忘°
不思量自难忘° 2020-12-12 07:44

This code gives me an error message: [Error] Unit1.pas(52): Undeclared identifier: \'Edit1\'.

procedure SetTCPIPDNSAddresses(sIPs : String);
begin
          


        
相关标签:
2条回答
  • 2020-12-12 07:58

    If you cannot use the solution that Ken White gave you, for instance if you are not allowed to change the signature of SetTCPIPDNSAddresses(), then another option is to access the TEdit via the global pointer to its parent TForm (if your TForm instance is actually making use of that pointer, that is), eg:

    procedure SetTCPIPDNSAddresses(sIPs : String); 
    begin 
      SaveStringToRegistry_LOCAL_MACHINE( 
        'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' + Form1.Edit1.Text, 
        'NameServer', sIPs); 
    end; 
    
    0 讨论(0)
  • 2020-12-12 08:17

    Your code isn't a method of the form, and therefore has no access to Edit1.

    Either make it a form method:

    type
      TForm1=class(TForm)
      ...
      private
        procedure SetTCPIPDNSAddresses(sIPs : String);
      ...
      end;
    
    implementation
    
    procedure TForm1.SetTCPIPDNSAddresses(sIPs : String);
     begin
       ...
     end;
    

    Or change it to accept the contents of Edit1.Text as another parameter:

    procedure SetTCPIPDNSAddresses(sIPs : String; RegName: String);
    begin
      SaveStringToRegistry_LOCAL_MACHINE(
        'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' + 
         RegName, 'NameServer', sIPs);
    end;
    

    And call it like:

    SetTCPIPDNSAddresses(sTheIPs, Edit1.Text);
    
    0 讨论(0)
提交回复
热议问题