Squall_FF8 1 Posted 15 hours ago Hey guys, I have a very weird situation: type tModel = record Field: string; Control: TWinControl; Kind: integer; end; tModels = class private FList: TList<tModel>; public procedure DoSomething; ... end; procedure tModels.DoSomething; begin FList[1].Field := 'somthing'; end; When I try to compile I get error: [dcc32 Error] E2064 Left side cannot be assigned to Why I get this error? That would mean I cant have direct access to the fields of the record for manipulation ... P.S. FList[1] := Value; where Value is tModel works. Share this post Link to post
Anders Melander 1995 Posted 15 hours ago 7 minutes ago, Squall_FF8 said: When I try to compile I get error: [dcc32 Error] E2064 Left side cannot be assigned to Why I get this error? Because the TList<T>.Items property getter returns a read-only copy of the record. This is the same for all properties that return a record. You can use the TList<T>.List property to gain access to the internal array of TList<T>. 1 Share this post Link to post
Squall_FF8 1 Posted 12 hours ago 2 hours ago, Anders Melander said: You can use the TList<T>.List property to gain access to the internal array of TList<T>. Thank you! Typing things like Models.List.Field defy my goal to access an object as array: Models.Field, but at least works Later on if I have time I might experiment to change the TList with dynamic array Share this post Link to post
Anders Melander 1995 Posted 12 hours ago If you mean to expose the TModel record from the TModels list encapsulation then you can do so by (also) exposing record pointers: type TModel = record Foo: string; Bar: integer; end; PModel = ^TModel; TModels = class private FItems: TList<TModel>; private function GetModel(Index: integer): TModel; procedure SetModel(Index: integer; const Value: TModel); function GetData(Index: integer): PModel; public property Items[Index: integer]: TModel read GetModel write SetModel; default; property Data[Index: integer]: PModel read GetData; end; function TModels.GetModel(Index: integer): TModel; begin Result := FItems[Index]; end; procedure TModels.SetModel(Index: integer; const Value: TModel); begin FItems[Index] := Value; end; function TModels.GetData(Index: integer): PModel; begin Result := @FItems[Index]; end; var Models: TModels; var Model: TModel; ... Model := Models[0]; ... Models[0] := Model; ... Models.Data[0].Foo := 'Hello world'; Models.Data[0].Bar := 42; Share this post Link to post