how to set array length with delphi 2010 rtti [Edit]

how to set array length in runtime ? setLength(t.GetProperty('Propertys'),3); ????



{code:delphi}
unit Unit3;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type

    TSubProperty = Class
      private
        Fitem2: Integer;
        Fitem1: String;
        procedure Setitem1(const Value: String);
        procedure Setitem2(const Value: Integer);
      published
      property item1:String read Fitem1 write Setitem1;
      property item2:Integer read Fitem2 write Setitem2;
    End;

    TArraySubProperties=array of TSubProperty;

    TmyObject = Class
      private
        FProperties: TArraySubProperties;
        procedure SetProperties(const Value: TArraySubProperties);
      published
      property Properties:TArraySubProperties read FProperties write SetProperties;
    End;


      TForm3 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form3: TForm3;

    implementation

    {$R *.dfm}

    procedure TForm3.Button1Click(Sender: TObject);
    var
    myObject:TmyObject;
    ctx : TRttiContext;
    t : TRttiType;
    obj:TObject;
    begin
    myObject :=TmyObject.Create;
    ctx := TRttiContext.Create;
    t := ctx.GetType(myObject.ClassType);
   // setLength(t.GetProperty('Properties'),3); ????????????????????????????????????
   obj:= (t.GetProperty('Properties').PropertyType as TRttiDynamicArrayType).ElementType.AsInstance.MetaclassType.Create;
  //showmessage(obj.toStirng); --> TSubProperty 
   t.GetProperty('Properties').getValue(myObject).setArrayElement(0,obj);

   obj:= (t.GetProperty('Properties').PropertyType as TRttiDynamicArrayType).ElementType.AsInstance.MetaclassType.Create;
   t.GetProperty('Properties').getValue(myObject).setArrayElement(1,obj);

   obj:= (t.GetProperty('Properties').PropertyType as TRttiDynamicArrayType).ElementType.AsInstance.MetaclassType.Create;
   t.GetProperty('Properties').getValue(myObject).setArrayElement(2,obj);

{
    myObject.Properties[0] :=TSubProperty.Create;
    myObject.Properties[0].item1 :='x';
    myObject.Properties[0].item2 :=1;

    myObject.Properties[1] :=TSubProperty.Create;
    myObject.Properties[1].item1 :='y';
    myObject.Properties[1].item2 :=2;


    myObject.Properties[2] :=TSubProperty.Create;
    myObject.Properties[2].item1 :='z';
    myObject.Properties[2].item2 :=3;


    ShowMessage(myObject.Properties[2].item1);

    FreeAndNil(myObject.Properties[2]);
    FreeAndNil(myObject.Properties[1]);
    FreeAndNil(myObject.Properties[0]);
}
    FreeAndNil(myObject);

    end;

    { TSubProperty }

    procedure TSubProperty.Setitem1(const Value: String);
    begin
      Fitem1 := Value;
    end;

    procedure TSubProperty.Setitem2(const Value: Integer);
    begin
      Fitem2 := Value;
    end;

    { TmyObject }

    procedure TmyObject.SetProperties(const Value: TArraySubProperties);
    begin
      FProperties := Value;
    end;

    end.{code}

Edited by: sadettin polat on Nov 20, 2009 2:38 PM
0
sadettin
11/20/2009 10:39:03 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

2 Replies
955 Views

Similar Articles

[PageSpeed] 31

<sadettin polat> wrote in message news:185641@forums.codegear.com...

> how to set array length in runtime ? setLength(t.GetProperty('Propertys'),3); ????

You should not set the length of an array-based property directly.  In fact, the component should not expose access to the array directly at all, ie:

{code:delphi}
type
  TmyObject = Class
  private
    FProperties: TArraySubProperties;

    function GetPropertiesLength;
    procedure SetPropertiesLength(Value: Integer);
    function GetProperty(Index: Integer): TSubProperty;
  public
    destructor Destroy; override;
    property PropertiesLength: Integer read GetPropertiesLength write SetPropertiesLength;
    property Properties[Index: Integer]: TSubProperty read GetProperty default;

  end;

destructor TmyObject.Destroy;
begin
  SetPropertiesLength(0);
  inherited;
end;

procedure TmyObject.SetPropertiesLength(Value: Integer);
var
  OldLen, I: Integer;
begin
  if Value < 0 then Value := 0;
  OldLen := Length(FProperties);
  if Value = OldLen then Exit;
  if Value < OldLen then
  begin
    for I := 0 to Value-1 do
      FProperties[I].Free;
  end;
  SetLength(FProperties, Value);
  if Value > OldLen then
  begin
    for I := OldLen to Value-1 do
      FProperties[I] := TSubProperty.Create;
  end;
end;

function TmyObject.GetProperty(Index: Integer): TSubProperty;
begin
  Result := FProperties[Index];
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
11/20/2009 10:11:09 PM
Classes are defined by wsdlimporter. I have no chance of making any changes.

> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <sadettin polat> wrote in message news:185641@forums.codegear.com...
> 
> > how to set array length in runtime ? setLength(t.GetProperty('Propertys'),3); ????
> 
> You should not set the length of an array-based property directly.  In fact, the component should not expose access to the array directly at all, ie:
> 
> {code:delphi}
> type
>   TmyObject = Class
>   private
>     FProperties: TArraySubProperties;
>
0
sadettin
11/20/2009 10:35:16 PM
Reply:

Similar Artilces:

Delphi 2010 RTTI Set length dynamic array [Edit]
Good day, I have a problem, i had searched this on google, and EDN but i didn't find a good source, or complet. var x,y:TValue; a:array [0..100] of integer; begin x:=TValue.From(v); y:=TValue.From(a); y.SetArrayElement(0,2); y.SetArrayElement(1,2); end; It works. because the compiler had allocated 100*32 bits in the heap memory. How i will procede for a dynamic array a:array of integer? I want: var y:TValue; a:array of integer; begin y:=TValue.From(a); y.SetArrayElement(y.GetArrayLength+1); Thank you very much and have a nice day. ...

Update Delphi 2005 to Delphi 2010, which do i need? [Edit]
Hello, we have one Delphi 2005 Professional here and want to upgrade to Delphi 2010, but we are a bit confused. Our distributor in Germany, ingram-micro offers several upgrade products: UPG DELPHI 2010 PRO - NAMED USER UPG DELPHI 2010 PRO - NAMED USER ELS UPG DELPHI 2010 PRO - NAMED USER FLEX RAD STUDIO 2010 PRO - UPG UPG RAD STUDIO 2010 PRO - NAMED ELC UPG RAD STUDIO 2010 PRO - NAMED FLEX UPG DELPHI 2010 PRO - CONCURRENT ELS UPG DELPHI 2010 PRO - CONCURRENT FLEX What do they mean exactly and which one do i need? At the moment delphi is installed on two PCs and we want to use ...

Code works in Delphi 7 but not in Delphi 2010 [Edit]
hello, i have a procedure that open's a file by passing the file name as the parameter to the executable. something like this {code} C : \ P r o g r a m F i l e s \ Da c k e r \ D r a c k e r . e x e " G : \ D E l p h i 7 \ D e l p h i 7 A p p _ l o g . t " {code} The source code is {code} procedure OpenFileWithExe var hReg: HKEY; Ret: Longint; RegDataType, RegDataSize: DWORD; CmdLine: array [0..560] of Char; Len: Integer; SInfo: TStartupInfo; PInfo: TProcessInformation; begin Ret := windows.RegOpenKeyEx(HKEY_CURRENT_USER, ...

Delphi 2010 produces much larger binaries than Delphi 2009 [Edit]
When I compile my project with Delphi 2010 the resulting executable is about 30% larger than the one compiled with Delphi 2009. Both binaries are compiled with the same source code and project options. The size of individual DCU files increases by 5-20%. Has anybody experienced similar results? Has anybody an idea what could cause this? It surprises me, as the changes in syntax and VCL have not been that big. Best regards, Joachim Edited by: Joachim Marder on Sep 25, 2009 6:07 AM "Joachim Marder" wrote in message news:166152@forums.codegear.com... > When I compi...

from delphi 6 to delphi 2010
Hi. It is possible, with component RX, dxforumlibrary, InfoPower3000Pro, StringAlignGrid. Accepts communication BDE. Thank by comments. excequiel arostica wrote: >Hi. > It is possible, with component RX, dxforumlibrary, >InfoPower3000Pro, StringAlignGrid. Accepts communication BDE. > >Thank by comments. Rx is dead and sources are taken over by jcl/jvcl. I dont know about the rest of the components and i have no experiences with bde over the last 9 years. excequiel arostica wrote: > Hi. > It is possible, with component RX, dxforumlibrary,...

Delphi XE / Delphi 2010
Hello! I noticed that Embarcadero® Delphi® 2010 Version is not on the list of products on Embarcadero page. Or is it still possible to buy it? Will RAD Studio XE compile programs written in Delphi 2010 without problems.? Thanks. Am 13.09.2010 09:04, schrieb Petra Nemec: > Will RAD Studio XE compile programs written in Delphi 2010 without problems.? As always you will probably have to recreate the projects as the import is still a bit -- special. Christian Hello! Does anybody know if it is still possible to get a Delphi2010 trial version (if yes where)? ...

SEPA components for Delphi with Source Code (Delphi 5
Hi all, in the european union change next year the Bankingformat to the SEPA Format. All peoples and companies must change the bankingssoftware and the costumer data form acountnummers in the new IBAN and BIC numbers. See: http://www.arma-it.de/shop/artikelueber.php?wgruppeid=211&wgruppe_offen=211 Functions: - generate SEPA XML'S - Calc IBAN - BIC Database (DE,AT and CH) Questions: vertrieb@arma-it.de PS: Bankinssoftware for Develpoers (Germany only) http://www.arma-it.de/shop/artikelueber.php?wgruppeid=212&wgruppe_offen=212 El 26/10/13 21:38, A...

Price of upgrade from Delphi 2010 Professional to Delphi 2010 Enterprise?
It's weird, but I could not find the answer on so simple question not in Embarcadero Shop, neither by the forum search. For example, the shop info about Enterprise upgrade says: The Upgrade product is available to registered owners of 2006-2009 versions, blah-blah... So, if I'm Delphi 2010 Professional user, I even not eligible for upgrade? I have to buy a new version of Enterprise??? But if I'm Delphi 2006 Professional user, I can upgrade. Please clarify this nonsense for me. Exact question is in the subject. Thanks! Hi Dmitri, > It's weird, but I could not...

Delphi 2010 RTTI doesn't recognize array type
I'm using Delphi 2010 and trying to traverse simple structure by using RTTI. Does anybody know why RTTI cannot recognize array type and return nil instead of actual type? Here is example: type R = record f1: array [0 .. 0] of byte; end; procedure Test; var context: TRttiContext; field: TRttiField; rttiType: TRttiType; begin context := TRttiContext.Create; rttiType := context.GetType(TypeInfo(R)); for field in rttiType.GetFields do if field.FieldType = nil then Writeln('Type is nil.'); context.Free; end; Andrey Tihankov wro...

Free RAD Studio 2010 and Delphi Prism 2011 with purchase of Delphi 2010
Hi All, For those of you who complain that Delphi (and RAD Studio) is too expensive... for a few short weeks there are two overlapping special offers from Embarcadero that you can use to move up to RAD Studio 2010 including the latest Delphi Prism 2011 for the price of a single Delphi 2010 (New User or Upgrade)... The first "RAD Studio PROMO" offer is still valid until June 28th: when you order Delphi 2010, you get RAD Studio 2010 delivered instead (including Delphi 2010, C++Builder 2010 and Delphi Prism 2010). As of May 26th, the "Delphi Prism 2011" of...

Delphi 2010 w Delphi 2007
I have D2007 installed on my laptop (XP sp3 running on dual core Athlon). Can I install D2010 on this machine without it affecting D2007? Will D2010 affect D2007 in any way? Thanks, Randall Carpenter > {quote:title=Randall Carpenter wrote:}{quote} > I have D2007 installed on my laptop (XP sp3 running on dual core Athlon). > Can I install D2010 on this machine without it affecting D2007? Will D2010 > affect D2007 in any way? Won't hurt a thing. I have D7, RAD Studio 2007, RAD Studio 2009, and RAD Studio 2010 all on my desktop system and they coexist fine. Jus...

Migrate from Delphi 2007 to Delphi 2010
Hi All, Thanks in advance for your help. Below is my query, Currently I am using Delphi 2007 and i want to migrate to Delphi 2010. 1. What all things i need to take care while doing this? 2. What all third party components will get impacted? 3. Any known issues in Delphi 2010 which might impact the cause? 4. Any changes in database operation required as i am using Oracle? Hope I have post this query under correct category. If not sorry for the trouble and could you please suggest me the correct category for this? Looking forward for your response. Have a great day. Th...

Using Delphi 7 and Delphi 2010
I had a project in Delphi 7. I worked on it in Delphi 2010. When I try to open the .dpr again in Delphi 7, the IDE toolbar widens across both monitors, and Delphi 7 locks up. It appears that Delphi 2010 modifies the .dpr in a way that makes Delphi 7 unable to open it again. All my forms & units appear to open fine in D7 after D2010, except for the warnings on the ExplicitHeight and ExplicitWidth properties being missing... I can easily recreate the .dpr, but is this expected behavior? Thanks. Tom Field wrote: > I had a project in Delphi 7. > > I worked o...

Delphi 2010 professional to Delphi XE3
Hello, Sorry for this is not right place to ask this question: I have Delphi 2010 professional version, I like to upgrade to Delphi XE3 Am eligible to upgrade or no not? I like answer from Delphi sales team. I couldn’t find email to ask them. Thanks Mike Thomas <> wrote in news:506459@forums.embarcadero.com: > Hello, > > Sorry for this is not right place to ask this question: > I have Delphi 2010 professional version, I like to upgrade to Delphi > XE3 Am eligible to upgrade or no not? I like answer from Delphi > sales team. I couldn’t find emai...

Web resources about - how to set array length with delphi 2010 rtti [Edit] - embarcadero.delphi.general

Length - Wikipedia, the free encyclopedia
In the International System of Quantities , length is any quantity with dimension distance. In other contexts "length" is the measured dimension ...

Sunrise streaker the latest prankster to go to extreme lengths to get on telly
It's a fine Spring Racing Carnival tradition: interrupting live TV broadcasts by waving like an idiot, bellowing something mates, or ripping ...

Keep ASIC reviews at arm's length from vested interests
Almost 18 months after a damning Senate report into the performance of ASIC, three separate reviews are underway, with varying concerns about ...

Jennifer Lawrence in the full-length ‘Joy’ trailer: Oscarworthy or schmaltzy?
Jennifer Lawrence stars in the new Joy trailer, which dropped yesterday from 20th Century Fox. At one point in the film, JLaw plays a 40-year-old ...

Subway And Shortchanged Sandwich-Eaters Settle 2013 Lawsuit Over Footlong Sub Length
... justice for your complaints. This case combined seven separate ones, which were filed after a flurry of publicity in 2012 over sandwich lengths. ...

'Dragon's Lair' hits Kickstarter in search of a feature-length movie
That Dragon's Lair debuted as a LaserDisc format game/interactive movie but was never a proper film was always a little weird, but that could ...

The First Full-Length 'American Crime Story' Trailer Is Here and It's Totally Intense
The preview of Ryan Murphy's true crime series reveals the first look at 'The People vs. O.J. Simpson.'

It's Body Length, Not Mass, That Lets the Cheetah Run So Fast
... whale’s 31 MPH, and a three-toed sloth runs less than one MPH . A new paper by a pair of French physicists concludes that it’s the body length, ...

Foot-Long? Judge OKs Settlement Over Subway Sandwich Length
Judge approves settlement over Subway sandwich length in cases started by social media post

Increasing cable length in precision video applications
Transmitting video signals over long distances can be tricky. Cheap co-ax cables won’t do the job. You either need amplifiers along the path, ...

Resources last updated: 12/15/2015 1:06:47 AM