안녕하세요이 코드에 문제가 있습니다델파이 typinfo SetPropValue
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ActnList, StdCtrls, Buttons, MSObjCtrls, StrUtils;
type
Data = class(TObject)
FName : string;
FValue : string;
private
public
published
property Name : string read FName write FName;
property Value : string read FValue write FValue;
end;
type
TForm1 = class(TForm)
edtResult : TMSObjectText;
btnGo : TMSBitBtn;
ActionList1 : TActionList;
acGo : TAction;
procedure acGoExecute(Sender : TObject);
private
procedure Split(Delimiter, S : string; Strings : TStrings);
public
{ Public declarations }
end;
var
Form1 : TForm1;
implementation
uses TypInfo;
{$R *.dfm}
procedure TForm1.Split(Delimiter, S : string; Strings : TStrings);
var
P, OldP : integer;
Token : string;
begin
if (Strings = nil) or (Length(S) = 0) or (Length(Delimiter) = 0) then
exit;
P := Pos(Delimiter, S);
OldP := 1;
while P > 0 do
begin
Token := Copy(S, OldP, P - OldP);
Strings.Add(Token);
OldP := P + 1;
P := PosEx(Delimiter, S, OldP);
end;
if P = 0 then
Strings.Add(Copy(S, OldP, Length(S)));
end;
procedure TForm1.acGoExecute(Sender : TObject);
var
Lst, tmpLst : TStringList;
i : Integer;
Obj : Data;
str : string;
begin
str := 'Name=Jordan Borisov;Value=man';
Lst := TStringList.Create;
tmpLst := TStringList.Create;
Split(';', str, Lst);
Obj := Data.Create;
for i := 0 to Lst.Count - 1 do
begin
Split('=', Lst[i], tmpLst);
try
SetPropValue(Obj, tmpLst[0], tmpLst[1]);
except
ShowMessage(Format('Invalid property name %s', [tmpLst[0]]));
end;
tmpLst.Clear;
end;
edtResult.Text := 'Name[' + Obj.Name + '],Value[' + Obj.Value + ']';
end;
end.
사람이 문제가 어디 말해 수 있습니까?
미리 감사드립니다.
작동하지 않는 것을 알려 주시면 더 나은 답변을 받으실 수 있습니다. 컴파일되지 않습니까? 당신이 원하는대로하지 않습니까? 질문이 뭐야? –
깜빡해서 죄송합니다. 문제는 SetPropValue 메서드에서 Data 클래스의 속성 Name에 새 값을 설정하려고하지만 Exception은 텍스트가 매번 발생한다는 것입니다. 속성 이름이 없습니다. –
acGoExecute에서 Lst 및 tmpLst를 자유롭게 사용해야합니다. –