Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi TComboBox and string object (https://www.delphipraxis.net/166458-tcombobox-string-object.html)

WojTec 14. Feb 2012 13:53

Delphi-Version: 2010

TComboBox and string object
 
Add with function():

Delphi-Quellcode:
AStrings.AddObject(Name, TObject(string(Path)))
;

Test in OnChange:

Delphi-Quellcode:
ShowMessage(string(cbList.Items.Objects[cbList.ItemIndex]));


First ShowMessage() is showing path and then raises EInvalidPointer. How to fix it?

Medium 14. Feb 2012 14:14

AW: TComboBox and string object
 
The most direct way: Use a global variable for your string. But that can still end up in all sorts of messes because of the compiler magic strings come with. Especially fun stuff like copy-on-write related things.
(Edit, to be clear: Do NOT do this! It may work in some special and simple cases, but is dirty like whatnot.)

A more sensible way: Define a TStringList in a suitable scope, and put the index of your desired string from that list into your Object-Property. To read it, just use that index on your StringList. You'll save yourself a lot of potential trouble that way ;)

DeddyH 14. Feb 2012 14:20

AW: TComboBox and string object
 
Or simply use a real TObject containing a string property :)

himitsu 14. Feb 2012 14:57

AW: TComboBox and string object
 
Do not cast string, dyn. array oder interfaces. :warn:

This bypasses the reference count.

WojTec 15. Feb 2012 11:05

Re: TComboBox and string object
 
Thanks, I created class for this purpose :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz