Board index » cppbuilder » TComboBox->AddObject

TComboBox->AddObject


2007-06-08 12:10:36 AM
cppbuilder75
I want to add an AnsiString as the TObject of a ListBox. Is this the best
method?
neList->Items->AddObject(ne->Text,(TObject *)new AnsiString(neTypes->Text));
If so, how do I extract the AnsiString later? I tried a reinterpret_cast<AnsiString>(neList->Items->Objects[i])
but that produced an error saying that I cannot cast from a TObject to an
AnsiString. If I change <AnsiString>to <AnsiString *>, I don't get any compilation
errors but I dont get the text either.
Last, do I need to delete the new AnsiString manually or is that handled
automatically when I clear the original list?
Thanks in advance,
Brian
 
 

Re:TComboBox->AddObject

Brian Plotkin < XXXX@XXXXX.COM >wrote:
Quote

I want to add an AnsiString as the TObject of a ListBox. Is
this the best method?
No, because of memory management and it's extreemly susseptable
to leaks and AV's.
At the least, I would add a TStringList to hold the additional
AnsiString and store it's Index as the Object. That way, when
the ListBox is destroyed, you don't need to iterate the Objects
and cast to an AnsiString* and delete it - all you'd have to
do is delete the TStringList.
Another (VCL) approach would be to use DynamicArray. It manages
all of the allocation and deallocation automatically. If you
need to reuse it, simply setting it's Length to zero frees
everything but that's not needed because you can replace any
given element at any time.
The disadvantage to using a DynamicArray is that if you don't
know ahead of time how large you need it, every time you change
it's Length, it reallocates the entire array + 1 and copies
the old to the new and then destroys the old. This can reak
havoc on your memory in terms of fragmentation. Of course you
could always implement a smater allocation scheme to allocate
in bigger chuncks but then you have to keep track of what
you've actually used.
The same is also true for a std::vector except that it has a
smarter 'grow' method but it's usage is not as intuitive for a
VCL user that's never used it before.
My personal choice would be to use a TStringList. TStrings is
a very powerful class and I've used it to generate (small)
relational data bases (using flat files!) based on the PICK OS
where no records actually physically exist on disk and are
only realized in memory. For example:
Quote
neList->Items->AddObject(ne->Text,(TObject *)new AnsiString(neTypes->Text));
That would look something like:
neList->Items->AddObject( ne->Text, reinterpret_cast<TObject*>(pList->Add(neTypes->Text)) );
Then to get the string back out, that would depend if you have
the text for the neList Item or if you have the actual Index
into neList. For example, if you have the Index:
AnsiString S = pList->Strings[ reinterpret_cast<int>(neList->Objects[Index]) ];
and if you an Item from the neList:
AnsiString S = pList->Strings[ reinterpret_cast<int>(neList->Objects[neList->IndexOf(SomeItem)]) ];
In addition, given a pList Item, you can also find out what
particular neList Item it's associate with. For example:
AnsiString S = neList->Strings[ neList->IndexOfObject( reinterpret_cast<TObject*>(pList->IndexOf(SomeItem)) ];
~ JD
 

Re:TComboBox->AddObject

Hello JD,
Thank you very much for the information and suggestion. I will implement
it immediately!
 

{smallsort}

Re:TComboBox->AddObject

"Brian Plotkin" < XXXX@XXXXX.COM >wrote in message
Quote
Is this the best method?
"best" is subjective. There are many ways to accomplish what you ask.
I would suggest using a separate TStringList instead, and then store
indexes for that list into the ComboBox.
Quote
If so, how do I extract the AnsiString later? I tried a
reinterpret_cast
<AnsiString>(neList->Items->Objects[i]) but that produced an error
saying that I cannot cast from a TObject to an AnsiString.
That is correct. You stored an AnsiString* pointer into the Object,
so that is what you need to cast back to.
Quote
If I change <AnsiString>to <AnsiString *>, I don't get any
compilation
errors
That is the correct thing to do.
Quote
but I dont get the text either.
Yes, you do. You just have to dereference the pointer.
Quote
Last, do I need to delete the new AnsiString manually
Yes, because you allocated them manually.
Quote
is that handled automatically when I clear the original list?
No.
Gambit
 

Re:TComboBox->AddObject

Quote
>
Yes, you do. You just have to dereference the pointer.

Can you give me an example of dereferencing the pointer?
 

Re:TComboBox->AddObject

"Brian" < XXXX@XXXXX.COM >wrote in message
Quote
Can you give me an example of dereferencing the pointer?
neList->Items->AddObject(ne->Text, (TObject *) new
AnsiString(neTypes->Text));
...
AnsiString s = * (AnsiString*) neList->Items->Objects[index];
Gambit