Jump to content

How to avoid using GlobalVars in components development?


elGringo

Recommended Posts

Ok, to demonstrate problem, I have following structure (pic №1 in Attachment)

 

-SomeComponent.bpl

    -uSomeComponent_BPL.pas

    -uAdditional.pas

 

-TestProgram.exe

  -uTestProgram.pas

 

 

In uSomeComponent_BPL.pas I have special published property SomeField (Pic №2 in Attachment)

...
property SomeField:string read FSomeField write FSomeField;
...

What is my problem and my Task?

 

When user will enter Some Value in SomeField I need to transfer and use it in unit uAdditional of component. I cannot do that without using GlobalVars for the moment.

 

What did I try?

 

After register of Component, I tried to Create Locally exemplar of TSomeComponent from uSomeComponent_BPL like this

 

uAdditional...

procedure TfAdditional.FormCreate(Sender: TObject);

var SomeComponent:TSomeComponent;

begin
    SomeComponent:=TSomeComponent.Create(Self);
    ShowMessage(SomeComponent.SomeField);


end;

In uSomeComponent_BPL.pas

procedure TSomeComponent.CreateAdditionalForm;

var AdditionalForm:TfAdditional;

begin

    AdditionalForm:=TfAdditional.Create(Self);

end;

Added component to form in test program. Then in object inspector I entered some test value like '123'. After that in test program I tried to call that externally like this

procedure TfMainform.SomeComponent1Click(Sender: TObject);
begin
SomeComponent1.CreateAdditionalForm;
end;

But message was emty - so that didn't work...

 

If to use Global Vars - everything will work. But it is known that GlobalVars - bad practice. 

 

Conclusion

 

If you know how to transfer data from unit to unit in that case, please help me.

Regards, Stanislav. 

post-2378-0-65088900-1466708980_thumb.jpg

post-2378-0-94319300-1466709906_thumb.jpg

324_GlobalVarTest.zip

Link to comment
Share on other sites

Hi ElGringo,

Firts to all, sorry for my english, i'll try to explain me.

1) You have a component into your Test Program... this INSTANCE is named SomeComponent1 and his SomeValue property its set to "123"
2) When you call the CreateAdditionalForm Method, you delcare a new variable of TSomecomponent class, and you INSTANCES A NEW OBJECT
3) When you call ShowMessage, you pass the New INSTANCE recently created, which SomeValue property is empty.

I suggest:

1) Delete the OnCreate Method from your TAdditional Form
2) Create a New Private variable FSomeComponent
3) Create a overload constructor for this form,like this:
 

  TfAdditional = class(TForm)
  private
    { Private declarations }
    FSomeComponent: TSomeComponent;
  public
    { Public declarations }
    Constructor Create(aSomeComponent: TSomeComponent);
  end;

 
4) Implement it, like this:
 

constructor TfAdditional.Create(aSomeComponent: TSomeComponent);
begin
  inherited Create(Application);
  FSomeComponent:= aSomeComponent;
  ShowMessage(FSomeComponent.SomeField);
end;

 

In this way, when you creates this Form, pass the Component "parent"

5) Finally, into the CreateAdditionalForm method write this:

procedure TSomeComponent.CreateAdditionalForm;

var AdditionalForm:TfAdditional;

begin

    AdditionalForm:=TfAdditional.Create(Self);

end;


I hope I have understood and solved your problem,

 

(again, sorry for my english)

Bye

Gatosoft

  • Upvote 1
Link to comment
Share on other sites

Hi, gatosoft! Yes! your answer is 100 % of what I need for the moment !!!

 

Yesterday i found a little bit different decision, but yours is more elegant.

 

My decision was lite that

 

uSomeComponent_BPL

procedure TSomeComponent.MyClick(Sender: TObject);
begin


  fAdditionalLocal:=TfAdditional.Create(Self);
  fAdditionalLocal.somefieldInternal:=Self.SomeField;

  ShowMessage(fAdditionalLocal.somefieldInternal);

end;

but it is not so convenient as yours one!!! Thank you again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...