Reputation: 455
I have a class of type TObject called CModelItem
I want to have a list of these objects and be able to modify the values of each one
So, I created a class
CQueueList = class(TList)
private
public
end;
and I make
QueueList : CQueueList;
in var
Now, I can add the CModelItem to this list, like so:
QueueList := CQueueList.Create;
for idx := 0 to ndx - 1 do
begin
MyItem := CModelItem.Create;
MyItem.CopyHead(CModelItem(RunList.Objects[idx]));
MyItem.ReadData;
MyItem.NumOfIterations := NumRepEdit.Value;
MyItem.IsInQueue := True;
MyItem.LogEvents := EventsCheckBox.Checked;
MyItem.LogMatch := MatchCheckBox.Checked;
MyItem.LogUpdates := UpdatesCheckBox.Checked;
QueueList.Add(MyItem);
end;
I can also use it, so I can do:
DefForm := TRunDefForm.Create(Self, QueueList.Items[idx]);
with DefForm taking in a component and a CModelItem
But I'm running into problems trying to modify the values of an object in QueueL
First, I can't access something like MyItem.IsInQueue by doing
QueueList.Items[idx].IsInQueue := blah;
because it tells me IsInQueue is 'an undeclared identifier'
I've also tried making a new CModelItem and copying the information over, like this:
idx := QueueListBox.ItemIndex;
MyItem := QueueList.Items[idx];
and this compiles fine, but throws up an 'access violation error' when it goes into that function
I noticed that QueueList.Items[idx] is a pointer, but I'm really not sure how I should be accessing it
Upvotes: 1
Views: 172
Reputation: 37211
The compiler complains because TList.Items
returns an untyped pointer.
You can use a typecast:
CModelItem(QueueL.Items[idx]).IsInQueue := blah;
You can also reimplement the Items
property in your CQueueList
class:
private
function GetItems(Index: Integer): CModelItem;
public
property Items[Index: Integer]: CModelItem read GetItems; default;
end;
function CQueueList.GetItems(Index: Integer): CModelItem;
begin
Result := inherited Items[Index];
end;
As you've seen, using a local variable works; although the access violation is a bug probably somewhere else in your code.
Upvotes: 3