如何从另一个单元(runtimepackage)访问 属性(designTime 包)使用的私有变量
How to access to a private variable used as by a property ( designTime package) from another unit (runtimepackage)
为了创建一个组件,我创建了一个设计时包和运行时包,运行时包(我们称之为 RP140)包含我的组件的代码并需要 rtl.dcp,设计时包(我们称之为 DclRP140)包含注册程序,需要 DesignIDE、运行时包和 rtl.dcp。现在我需要访问属于 "DclRP140" 包的单元中声明的私有变量,从属于 "RP140" 的另一个单元,我创建了一个包含相关部分的简单代码,只是为了更容易了解:
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.
另一个单元包含我的组件的主要代码:
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.
首先,我必须告诉您,我是创建包和组件的新手,所以我的做法是否正确?或者有什么问题吗?
其次,正如我之前所说,我想做的是从 'myComponent' 访问 'MyComponentRegister' 中声明的私有变量,我尝试了很多技巧,但其中 none 有效,当然有办法做到这一点,但我无法找到它有限 experience.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.
此单元包含在设计时间和 运行 时间包中。
unit MyComponentRegister;
interface
uses
Classes, MyComponent;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
end.
第二个单元仅包含在您的设计时包中。请注意,它没有定义组件,因为它是在使用的 MyComponent
中定义的。
为了创建一个组件,我创建了一个设计时包和运行时包,运行时包(我们称之为 RP140)包含我的组件的代码并需要 rtl.dcp,设计时包(我们称之为 DclRP140)包含注册程序,需要 DesignIDE、运行时包和 rtl.dcp。现在我需要访问属于 "DclRP140" 包的单元中声明的私有变量,从属于 "RP140" 的另一个单元,我创建了一个包含相关部分的简单代码,只是为了更容易了解:
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.
另一个单元包含我的组件的主要代码:
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.
首先,我必须告诉您,我是创建包和组件的新手,所以我的做法是否正确?或者有什么问题吗?
其次,正如我之前所说,我想做的是从 'myComponent' 访问 'MyComponentRegister' 中声明的私有变量,我尝试了很多技巧,但其中 none 有效,当然有办法做到这一点,但我无法找到它有限 experience.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.
此单元包含在设计时间和 运行 时间包中。
unit MyComponentRegister;
interface
uses
Classes, MyComponent;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
end.
第二个单元仅包含在您的设计时包中。请注意,它没有定义组件,因为它是在使用的 MyComponent
中定义的。