I want to use a variable from my main form in another form, each form has its own unit.
I want to use iUser from Login_u in Result_u
I found an article where they said i should put the variable in public declaration and under implementation 'uses and then the unit that wants to access the variable'. Also in the unit that wants to access that variable under implementation uses and then the unit name from where it wants to get the variable
unit Login_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls;
type
TfrmLogin = class(TForm)
btnLogin: TButton;
cbxUser: TComboBox;
procedure btnLoginClick(Sender: TObject);
private
{ Private declarations }
public
iUser:Integer;
{ Public declarations }
end;
var
frmLogin: TfrmLogin;
implementation
uses Result_u;
{$R *.dfm}
procedure TfrmLogin.btnLoginClick(Sender: TObject);
begin
iUser:= cbxUser.ItemIndex;
end;
end;
end.
In my result unit i get the error undeclared identififier, I used the activate procedure and a show message just as a test
unit Result_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, jpeg, ExtCtrls;
type
TfrmResult = class(TForm)
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmResult: TfrmUitslae;
implementation
uses Login_u;
{$R *.dfm}
procedure TfrmUitslae.FormActivate(Sender: TObject);
begin
ShowMessage(iUser);
end;
end.
I have read a few articles about this but I keep getting lost, I'm a highschool student so it doesnt need to be complex code.
TFrmLogin. Then you can get theiUserthrough that instance:frmLoginInstance.iUser. Usually you should not use the globalfrmLoginvariable (remove it) and declare it locally where you need it. Perhaps inside yourTFrmResultdeclaration. And also remove theuses Result_ufromLogin_u. - LU RDuses Result_u;from 'Login_u'. - "Also in the unit that wants to access that variable under implementation uses and then the unit name" - That's ok. --- What you're missing is to qualify the field ->ShowMessage(frmLogin.iUser);(of course it will fail since iUser is an integer). - Sertac Akyuz