Reputation: 539
in order to create a component, I created a designtime and runtime packages, runtime package (lets call it RP140) contains the code of my component and requires rtl.dcp, designtime package (lets call it DclRP140) contains register procedure and requires DesignIDE, runtime package and rtl.dcp. Now I need to access private variables declared in the unit that belongs to "DclRP140" package, from another unit that belongs to "RP140", I created a simple code which contains the relevant part, just to make it easier to understand:
unit MyComponentRegister;
interface
uses Classes, MyComponent;
type
TEvent = procedure(sender: TObject) of object;
TMyComponent = class(TComponent)
private
FMyproperty: String;
FMyEvent: TEvent;
public
constructor Create(AOwner: TComponent); override;
published
property myProperty: String read FMyproperty write FMyproperty
default initial_value;
property myEvent: TEvent read FMyEvent write FMyEvent;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
Constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited;
FMyproperty := initial_value;
end;
end.
the other unit contains the main code of my component:
unit myComponent;
interface
uses
SysUtils, Classes;
type
TMyComponent = class(TComponent)
public
procedure myProcedure(avalue: string);
end;
implementation
procedure TMyComponent.myProcedure(avalue: string);
begin
FMyproperty := avalue; // I want to access to FMyproperty
if assigned(Fmyevent) then // I want to access to fMyEvent
// do some work
end;
end.
So first, I have to tell you that I'm new to creating packages and components, so am I doing it the right way? or is there something wrong?
Second,as I said before, what I want to do is to access to private variables declared in 'MyComponentRegister' from 'myComponent', I tried many tricks but none of them worked, for sure there is a way to do that, but I cant find it with my limited experience.So, how I can I solve this one??
Upvotes: 0
Views: 571
Reputation: 613262
Your problem is that you are trying to define your component in multiple places. That's not possible. Your code declares two distinct classes. That's one more than you need.
Do it like so:
unit MyComponent;
interface
uses
Classes;
type
TEvent = procedure(sender: TObject) of object;
TMyComponent = class(TComponent)
private
FMyproperty: String;
FMyEvent: TEvent;
public
constructor Create(AOwner: TComponent); override;
procedure myProcedure(avalue: string);
published
property myProperty: String read FMyproperty write FMyproperty;
property myEvent: TEvent read FMyEvent write FMyEvent;
end;
implementation
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited;
end;
procedure TMyComponent.myProcedure(avalue: string);
begin
FMyproperty := avalue;
if assigned(FMyEvent) then
; // do some work
end;
end.
This unit is included in both design time and run time packages.
unit MyComponentRegister;
interface
uses
Classes, MyComponent;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
end.
This second unit is included only in your design time package. Note that it does not defined the component because that is defined in the MyComponent
which is uses.
Upvotes: 5