Discussion:
Access to object in TList
(too old to reply)
Stark
2012-06-21 12:55:10 UTC
Permalink
While scanning a dataset, I create an object (TBudget class) for each record
and I store this objects in a TList. I need access to each object in order
to assign a value to one of his fields. I don't know how to do it, since I
don't have a variable that references it.

while not MyTable.Eof do
begin
MyList.Add( TBudget.Create(...) );

// having created the object and having stored it in MyList, I need
to assign a value to the TBudget object.
// how do I make a reference to it ?

myObject.account:= MyTable.FieldByName('Acct').AsString; ?????

next;
end;
Hans-Peter Diettrich
2012-06-21 16:05:05 UTC
Permalink
Post by Stark
While scanning a dataset, I create an object (TBudget class) for each
record and I store this objects in a TList. I need access to each object
in order to assign a value to one of his fields. I don't know how to do
it, since I don't have a variable that references it.
Simply add such a local variable :-)
Post by Stark
while not MyTable.Eof do
begin
MyList.Add( TBudget.Create(...) );
Remember the item index, returned by MyList.Add. Or use the local variable:
MyBudget := TBudget.Create(...);
MyList.Add(MyBudget);
Post by Stark
// having created the object and having stored it in MyList, I
need to assign a value to the TBudget object.
// how do I make a reference to it ?
myObject.account:= MyTable.FieldByName('Acct').AsString; ?????
MyBudget.Account...
Post by Stark
next;
end;
DoDi
Stark
2012-06-22 13:08:10 UTC
Permalink
Ok, I did create a local variable and no problem. But I thought I could do
away with the variable and do something like:

TBudget ( MyList [..] ).Account :=
MyTable.FieldByName('Acct').AsString;

but I cannot find a way to have the current index of MyList.

You say "Remember the item index, returned by MyList.Add. ", but MyList is a
TList. Does it have an ItemIndex ?

This is only to satisfy a curiosity, and may be to learn something new ...
Hans-Peter Diettrich
2012-06-22 18:59:53 UTC
Permalink
Post by Stark
Ok, I did create a local variable and no problem. But I thought I could
TBudget ( MyList [..] ).Account :=
MyTable.FieldByName('Acct').AsString;
but I cannot find a way to have the current index of MyList.
Right, you have to know yourself what you consider the current item.
Post by Stark
You say "Remember the item index, returned by MyList.Add. ", but MyList
is a TList. Does it have an ItemIndex ?
What does Add return IYO?

DoDi
Jamie
2012-06-22 22:13:57 UTC
Permalink
Post by Stark
Ok, I did create a local variable and no problem. But I thought I could
TBudget ( MyList [..] ).Account :=
MyTable.FieldByName('Acct').AsString;
but I cannot find a way to have the current index of MyList.
You say "Remember the item index, returned by MyList.Add. ", but MyList
is a TList. Does it have an ItemIndex ?
This is only to satisfy a curiosity, and may be to learn something new ...
If you need work with indexes,...

You can use the IndexOf( ReturnedPointer).

CurrentIndex := Mylist.IndexOf(MyList.Add(ObjectToADd));

There are issues with this however.. If you were to delete and entry
prior to the CurrentIndex, the time scan for this, the index here will
be one less. At least that is the way it used to me.

Have you looked at "TobjectList" ? That has a little more handling
abilities..

Also, TStringList, allows you to assign object pointers and text to
each entry..

Jamie
Maarten Wiltink
2012-06-26 21:14:11 UTC
Permalink
"Jamie" <***@charter.net> wrote in message news:TY5Fr.50723$***@newsfe15.iad...
[...]
Post by Jamie
CurrentIndex := Mylist.IndexOf(MyList.Add(ObjectToADd));
There are issues with this however..
I'd say. TList.IndexOf expects a pointer; that would be ObjectToADd (sic).
TList.Add returns an index; that should become CurrentIndex.

Also, retrieving a pointer by index after determining the index by
adding the pointer is... pointless (groan). You know the pointer,
you've just added it. Incidentally, it's an object reference not a
pointer. There's a difference even if the two are assignment compatible.

I'm not sure if pointers and Integers are assignment compatible, so
hopefully the above code would not even compile.

Groetjes,
Maarten Wiltink

Loading...