How to free an integer object stored with text in a combobox (delphi 2009)?

Please can someone clear up my confusion.

In database appliocations I frequently store an integer primary key along with text in a combobox.
eg
  {code}
  ComboCustomer.Items.AddObject(custname,TObject(custID)); 
 {code}  


Qn. 
Doing it like that do I need to do anything special to free the CustID objects when I close or destroy the form, or is that only needed when I create objects separately  
eg  
 {code}
 MyKey := TObject.Create(CustID);
 ComboCustomer.AddItem(custname', MyKey ) ;
{code}


I ask this as I am getting access violations using the code commonly found on the Internet

{code}
procedure FreeObjects(const strings: TStrings) ;
var
  idx : integer;
begin
  for idx := 0 to strings.Count-1 do
  begin
    strings.Objects[idx].Free;
    strings.Objects[idx] := nil;
  end;
end;
{code}

and calling it with
{code}
 FreeObjects(ComboCustomer.items);
{code}
0
howard
10/2/2012 9:56:54 AM
embarcadero.delphi.vcl.using 2297 articles. 1 followers. Follow

3 Replies
311 Views

Similar Articles

[PageSpeed] 48

howard manwaring wrote:

> Please can someone clear up my confusion.
> 
> In database appliocations I frequently store an integer primary key along with text in a combobox.
> eg
>   {code}
>   ComboCustomer.Items.AddObject(custname,TObject(custID)); 
>  {code}  

That is ok, but note that the cast of custID to TObject is there only to satisfy the type that
AddObject expects, IOW an object reference. It does not change the integer into an object reference.

> Qn. 
> Doing it like that do I need to do anything special to free the CustID objects when I close or
> destroy the form,

The custID is still only an integer, it is not an object (reference), thus you do not need to do
anything at form or list destruction.

 or is that only needed when I create objects separately eg  
>  {code}
>  MyKey := TObject.Create(CustID);
>  ComboCustomer.AddItem(custname', MyKey ) ;
> {code}

In this case you would need to free the objects, because they would indeed be objects.

> I ask this as I am getting access violations using the code commonly found on the Internet
> 
> {code}
> procedure FreeObjects(const strings: TStrings) ;
> var
>   idx : integer;
> begin
>   for idx := 0 to strings.Count-1 do
>   begin
>     strings.Objects[idx].Free;
>     strings.Objects[idx] := nil;
>   end;
> end;
> {code}

You should not do this if you only store integers.

> and calling it with
> {code}
>  FreeObjects(ComboCustomer.items);
> {code}

Cheers
Tom



-- 
Tom Brunberg
firstname.surname@welho.com
0
Tom
10/2/2012 10:32:30 AM
Thank you both for your very fast and thorough answers. 
That clears it up nicely (annotating my specific questions was particularly helpful)
Regards
0
howard
10/2/2012 10:59:27 AM
For only storing a value with the text in a list you can use name-value strings:

http://www.delphibasics.co.uk/RTL.asp?Name=TStringList
(see second example)

For storing more values I would make my own object and add this to the list:

{code}
Type

TItemObject = Class(TObject)
        ID : Integer;
        Description : String;
end;

procedure FreeObjectsInList(aLst:TStrings; bClearList:Boolean);
var i:integer;
begin
  if aLst<>NIL then
  Begin
    for I := 0 to aLst.Count - 1 do
    begin
      if Assigned(aLst.Objects[i]) then
      begin
        aLst.Objects[i].Free;
        aLst.Objects[i]:=nil;
      end;
    end;
    if bClearList then
    begin
      aLst.Clear;
    end;
  End;
end;

Procedure FillList;
var ItemObject: TItemObject;
Begin
  ItemObject:=TItemObject.Create;
  ItemObject.ID=1;
  ItemObject.Description:='Description'

  MyList.AddObject('Text',ItemObject);
End;

Procedure DestroyList;
Begin
  FreeObjectsInList(MyList,True);
End;
{code}

Edited by: Robert Triest on Oct 2, 2012 12:59 PM
0
Robert
10/2/2012 11:00:05 AM
Reply:

Similar Artilces:

can i use delphi 2007 bpls in delphi 2009
can i use delphi 2007 bpls in delphi 2009 ? "Gaetan SIRY" skrev i meddelelsen news:8703@forums.codegear.com... > can i use delphi 2007 bpls in delphi 2009 ? No. -- Finn Tolderlund The compatiblity between D2006 and D2007 was unique. Normaly they can't be compatible because new version enhance/change the API. And D2009 defines String = Unicode where older version has String = Ansistring so D200x/Dx-BPL are 200% incompatible with D2009. ...

Delphi 2009
Hi All Sorry if this is a silly question answered many times before but does the Delphi 2009 really have no VCL source? And also .. does the Professional product version have the source? Many thanks in advance -- Ed. "Ed Weatherup" > Hi All > > Sorry if this is a silly question answered many times before but does the > Delphi 2009 really have no VCL source? > Hi, No. Trial versions come without source code. Ed Weatherup wrote: > Sorry if this is a silly question answered many times before but does > the Delphi 2009 really have ...

Delphi 4 to Delphi 2009
Hello, Thanks to all who answered my previous question. That was a great help. And atlast our client agreed to upgrade our delphi version from 4 to Delphi 2009. *Sigh*. But before that, I need to give the estimation and cost regarding the migration to delphi 2009. Can anyone tell me is there any tool to migrate from delphi 4 to delphi 2009 or just I need to compile our Delphi 4 application in Delphi 2009. I have read from the delphi 2009 feature matrix that Delphi 1 through Delphi 2007 import is possible in delphi 2009. But i am not that sure considering the size of our application. ...

Delphi 5 To Delphi 2009
I upgraded to Delphi 2009 from D5. The install says I can install Delphi and/or C++. Delphi installed OK but I see nothing of C++. What am I missing or does my upgrade not include C++? Thanks It depends on what you bought. If you bought Delphi 2009 only, that's what you get. If you bought Delphi 2009 and C++ Builder 2009 you get both. My guess is you got Delphi 2009 only. The simplest way to verify is look your invoice - it should say I would think. You could also go to members.embarcadero.com, login, then click on my registered products. There will be a textual description of...

Nice, Delphi XE includes free access to earlier version licenses and downloads for Delphi 2010, 2009, 2007 and 7.
Looking at the details for Delphi XE Professional while checking the prices etc and found the above as part of a paragraph. "Delphi XE includes free access to earlier version licenses and downloads for Delphi 2010, 2009, 2007 and 7. Details will be included with your order confirmation email and in the product readme." Very nice bonus! Brian > {quote:title=Brian Evans wrote:}{quote} > Looking at the details for Delphi XE Professional while checking the > prices etc and found the above as part of a paragraph. > > "Delphi XE includes free acce...

Upgrading to Delphi 2009 from Delphi 5
I've decided it's time to upgrade to Delphi 2009 Professional from my current Delphi 5 Professional which (according to the FAQ) makes me eligible for the upgrade pricing. However, I'd just like to clarify how this process works. My concern is that my Delphi 5 is not registered on the CodeGear site and this doesn't seem to be possible as far as I can tell. When installing the trial I noticed that Delphi 2009 registers itself with your CodeGear account so does this mean that the previous version must be registered there to verify you own a Professional edition? I plan to o...

Using Delphi 7 and Delphi 2010
I had a project in Delphi 7. I worked on it in Delphi 2010. When I try to open the .dpr again in Delphi 7, the IDE toolbar widens across both monitors, and Delphi 7 locks up. It appears that Delphi 2010 modifies the .dpr in a way that makes Delphi 7 unable to open it again. All my forms & units appear to open fine in D7 after D2010, except for the warnings on the ExplicitHeight and ExplicitWidth properties being missing... I can easily recreate the .dpr, but is this expected behavior? Thanks. Tom Field wrote: > I had a project in Delphi 7. > > I worked o...

Delphi 2009 alongside Delphi 2007
Hi all, I am about to install Delphi 2009 on the same development system as Delphi 2007, before I embark is there anything I should be aware of so as to not break my Delphi 2007 installation and projects ? Thanks JT John Taylor wrote: > Hi all, > > I am about to install Delphi 2009 on the same development system > as Delphi 2007, before I embark is there anything I should be aware > of so as to not break my Delphi 2007 installation and projects ? As long as you install into a separate folder there is no problem with the two versions getting into each other...

is there a Delphi 2007
Would be nice to have a bundle of the latest Delphi packages. Gilbert Padilla wrote: > Would be nice to have a bundle of the latest Delphi packages. > I expect Delphi 2007 will only be on sale until the full RAD Studio 2009 edition is published, including Delphi 2009.net. W ...

Delphi 2007 to Delphi 2009 Conversion
CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; How to convert to Delphi 2009? Bill Bill Miller wrote: > CharArrayPtr = ^CharArray; > CharArray = array[0..MaxInt-1] of Char; > > How to convert to Delphi 2009? > > Bill it depends heavelly on how it is used fearther down in your project and what you want to convert it to. I you want to simple convert the declarations ignoring UNicode altogether then the only think you have to change is the CharArray declaratin from a Char to an AnsiChar eg CharArrayPtr = ^CharArray; CharArray =...

Delphi 7 to Delphi 2009 conversion
Hello group, What do I need to do to comile the following code in D2009. Compiler error after the Else E2010: Incompatible Types 'AnsiChar' and 'Char' if isUnicode then SearchHandle := FindFirstFilew( PWideChar( fn ), FindDataW ) else SearchHandle := FindFirstFile( PAnsiChar( Ansistring( fn ) ), FindDataA ); <<<<<compiler error in the above line >>>>>>>>>>> Regards, Bryan > What do I need to do to comile the following code in D2009. > Compiler error after the Else > E2010: Incompati...

Cannot use delphi 2009
It seems that delphi will die from the new unicode, cause in our projects we strongly use strings in memory, the first issue is that the memory redouble and we calculate for 3 million rows 20-30 weeks to enable our projects for unicode but we do not need unicode, at all! The only thing to save the day is to switch off unicode! We need this, please! Oliver > {quote:title=Oliver Bollmann wrote:}{quote} > It seems that delphi will die from the new unicode, > cause in our projects we strongly use strings in memory, > the first issue is that the memory redouble and we ca...

ShareMem Delphi 6 --> Delphi 2009
I have just upgraded from Delphi 6 to Delphi 2009. My software uses about 20 files, plus literally dozens more plug-ins. Every project has ShareMem as its first unit and borlndmm.dll is distributed with the software. I am aware that the Borland Memory Manager was replaced to use FastMM quite some time ago. Will I have to rebuild every EXE and DLL in DElphi 2009 that uses ShareMem, or will supplying the new borlndmm.dll be enough? i.e. I would like to just update the main application EXEs and DLLs, but leave the plug-ins alone. Will this work? Thanks, Jason. -- Jason Sweby ...

Can delphi 2009 and delphi 7 be on the same computer?
Can I install and run delphi 2009 and delphi 7 separately on the same computer or will they interfer with each other? I have delpi 7 already so do I need to choose a new folder (not c:\Borland) when installing 2009? Lorne Anderson wrote: > Can I install and run delphi 2009 and delphi 7 separately on the same > computer or will they interfer with each other? > > I have delpi 7 already so do I need to choose a new folder (not c:\Borland) > when installing 2009? It works fine. You usually want to install the older version first. As for folders, the root folde...

Web resources about - How to free an integer object stored with text in a combobox (delphi 2009)? - embarcadero.delphi.vcl.using


Stack Overflow
Q&A for professional and enthusiast programmers

dBase - Wikipedia, the free encyclopedia
dBase II was the first widely used database management system (DBMS) for microcomputers . It is a relational database management system. It was ...

热门搜索 - 我的异常网
我的异常网之热门搜索专题提供权威的各类程序问题解决方案,欢迎您的到来。

Watermarked edit controls
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

AutoIt Forums
The AutoIt community forums.

3. Automation
Albert Gareev Education Experience Blog Archives Content policy Copyright Testing Stories Articles Weekend Testing Automation Chapters Problems ...

Does Windows 8 programming lack creativity?
... is a feature called OwnerDraw. What this means is that a number of UI elements (controls) in the operating system, such as a Listbox or Combobox ...

Restore Wallet Instuctions
Restore Wallet Instructions Introduction A wallet.aes.json downloaded from blockchain.info or emailed to on sign up contains everything needed ...

Resources last updated: 1/21/2016 4:01:34 PM