请教各位高手,我自己创建一个类:
TWorker = class(TObject)
private
FsWorkerName:string;
public
published
property sWorkerName:string read FsWorkerName write FsWorkerName;
end;引用下面语句赋值:
var NewWorker:TWorker;
begin
NewWorker:=TWorker.Create;
SetPropValue(NewWorker,'sWorkerName','shallowshrimp');
end;结果系统报错,为什么?
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EPropertyError with message 'Property sWorkerName does not exist'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
TWorker = class(TObject)
private
FsWorkerName:string;
public
published
property sWorkerName:string read FsWorkerName write FsWorkerName;
end;引用下面语句赋值:
var NewWorker:TWorker;
begin
NewWorker:=TWorker.Create;
SetPropValue(NewWorker,'sWorkerName','shallowshrimp');
end;结果系统报错,为什么?
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EPropertyError with message 'Property sWorkerName does not exist'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
var
NewWorker:TWorker;
begin
NewWorker:=TWorker.Create;
NewWorker.FsWorkerName := 'FsWorkerName';
ShowMessage(NewWorker.sWorkerName);
end;这样楼主应该明白了吧!
TWorker = class(TObject)
private
FsWorkerName:string;
public
published
property sWorkerName:string read FsWorkerName write FsWorkerName;
end;
{$M-}用{$M+}让编译器为TWorker产生RTTI,
或者让TWorker从TPersistent派生
begin
....
SetStrProp(Worker,'sWorkerName','工人名字')
end;
你的TWorker类已经表示是工人类了,如果在属性中还重复出现Worker则有点看起来不爽。建议你用Name代替sWorkerName
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function SetControlProp(ComStr, value: string): boolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function TForm1.SetControlProp(ComStr, value: string): boolean;
var
ComName, ComProp: string;
i: integer;
PropInfo: PPropInfo;
cp: TComponent;
begin
i := Pos('.', ComStr);
if i > 0 then
begin
ComName := copy(ComStr, 1, i-1);
ComProp := Copy(Comstr, i+1, 1000);
cp := FindComponent(ComName);
PropInfo := GetPropInfo(cp.ClassInfo, ComProp);
if Assigned(PropInfo) then
SetStrProp(cp, PropInfo, value);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
SetControlProp('button1.caption', 'hello');
end;end.
不知道各位能否帮我解决这个问题?
最近怎么样,好久没有联系了
最近想用这个wx1452来升颗星
本来想一直隐藏下去,,,
呵呵,可能是有点无聊吧
不过有很多问题弄不懂