Declare public global variable in Delphi

后端 未结 3 946
余生分开走
余生分开走 2021-01-01 21:47

Let\'s say I have two forms in a delphi project, I want to be able to access form1\'s variables from form2. Is there anyone to declare, say a \'public\' variable in form1 wh

3条回答
  •  天命终不由人
    2021-01-01 22:06

    the example shows Form1(main) and Form2(other) which has better use for organization;


    FORM1 interface declaration only;

    can be read from all forms;

    interface
    
    procedure oncreate(Sender: TObject);
    
    implementation
    uses Form2;
    
    procedure TForm1.oncreate(Sender: TObject);
    begin
      Form2.test1;
      //{Form2.test2} are not visible to Form1;
    end;
    

    FORM2 interface and implementation declarations;

    implementation declaration are local to the unit; cannot be read from all forms;

    interface
    
    procedure test1;
    
    implementation
    
    procedure test2; //declared under implementation;
    
    procedure TForm2.test1;
    begin
      ShowMessage('form2 test1 message');
    end;
    
    procedure TForm2.test2;
    begin
      ShowMessage('form2 test2 message');
    end;
    


    any procedure, function, type, variable can be local to the unit under implementation;
    it also makes intellisense(Ctrl+Space) clean from declarations used only to the unit;

提交回复
热议问题