Delphi 2010. I am trying to initialize class variables in the constructor of a class (A). Then I want to have a reference to that class throughout the functions of another class (B - the main Form). When I try to initialize the class using a local variable in a B function, all works but if I declare an instance of A in B's private or public areas or under var, it throws an eaccessviolation when Create is called. Having a ref to A global to B is absolutely essential in many programming tasks. Just cannot find a way to do it.
My code:
uses MyFavs;
type
TForm4 = class(TForm)
procedure FormActivate(Sender: TObject);
private
public
mfav : TMyFavs;
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
procedure TForm4.FormActivate(Sender: TObject);
begin
mfav.Create; //crash here
end;
TMyFavs is just dummy code that declares a private integer and assigns it value 1 in the constructor. Even if the constructor does nothing or there is no declared constructor, calling Create still throws the exception. The problem is not there, it must be in the declaration of myfav global to the class Form4 and this happens whether I put the declaration under the type's private, public or var. I know this is very basic but I cannot find the answer.