The parameter of the object is zero; it cannot determine why

I suppose this should be pretty straight forward, but I have no idea why this is not working.

I took the old code and some of the objects used in the dll become unmanageable. Several objects have the same procedure.

SetPropertyValue(propName,propValue:string);

Now these methods are basically giant operators if..elsethat check propNameand assign propValueif it matches the property of the objects:

if propName='name' then
  name:=propValue
else if propName='address' then
  address:=propValue

Etc.

Each time an object receives a new property (or the type of the property changes and, therefore, the passed parameter of the value must be different in different ways), this method needs to be updated - it is obvious that this should not be done.

, , , .

, . : Obj Main - Main - VCL , SetPropertyValue:

Obj

unit Obj;

interface

uses
  RTTI;

{$RTTI INHERIT}
type TmyObj = class(TObject)
  Name:String;
  Address:String;
  City:String;

  procedure SetPropertyValue(sPropName, sPropValue:String);
end;


procedure SetObjProperty(AObject : TObject; propName, propValue:String);

implementation

procedure SetObjProperty(AObject : TObject; propName, propValue:String);
var
  context:TRttiContext;
  rt: TRttiType;
  prop: TRttiProperty;
begin

  if not Assigned(AObject) then
    exit;

  context:=RTTIContext.Create;
  rt:=Context.GetType(AObject.ClassType);

  for prop in rt.GetProperties do
  begin

  //do some stuff 

  end;

  Context.Free;

end;

{ TmyObj }

procedure TmyObj.SetPropertyValue(sPropName, sPropValue: String);
begin
  SetObjProperty(self, sPropName, sPropValue);
end;

end.

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    edtPropName: TEdit;
    Label1: TLabel;
    edtPropValue: TEdit;
    btnGo: TButton;
    procedure btnGoClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Obj;

{$R *.dfm}

procedure TForm1.btnGoClick(Sender: TObject);
var
  myObj:TMyObj;
begin

  myObj:=TMyObj.Create;
  myObj.SetPropertyValue(edtPropName.Text, edtPropValue.Text);

end;

end.

obj.SetPropertyValue ( ), .

, , Obj.SetPropertyValyue, SetObjectProperty - rt .

, , , CONST, / .

, ? SetObjProperty , :

thisObj:=TMyObj.Create;
SetObjProperty(thisObj,sThisName, sThisValue);

nil obj .

!

+4
1

, ?

RTTI, TMyObj properties.

, :

program Project62;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  RTTI;

{$RTTI INHERIT}
type 
  TmyObj = class(TObject)
  private
    FName:String;
    FAddress:String;
    FCity:String;
  public
    property Name: String read FName write FName;
    property Address: String read FAddress write FAddress;

    procedure SetPropertyValue(sPropName, sPropValue:String);
  end;

procedure SetObjProperty(AObject : TObject; propName, propValue:String);
var
  context:TRttiContext;
  rt: TRttiType;
  prop: TRttiProperty;
begin
  if not Assigned(AObject) then begin
    WriteLn('Not assigned');
    exit;
  end;
  rt:= Context.GetType(AObject.ClassType);
  for prop in rt.GetProperties do
  begin
    if (propName = 'Name') then
      prop.SetValue(AObject,propValue)
    else if (propName = 'Address') then
      prop.SetValue(AObject,propValue);
  end;
end;

procedure TmyObj.SetPropertyValue(sPropName, sPropValue: String);
begin
  SetObjProperty(self, sPropName, sPropValue);
end;

var
  myObj:TMyObj;
begin

  myObj:= TMyObj.Create;
  myObj.SetPropertyValue('Name', '1');
  WriteLn(myObj.Name);
  ReadLn;
end.
+4

All Articles