?
Solved

How to store 2 int int TStrings?

Posted on 2003-03-09
12
Medium Priority
?
197 Views
Last Modified: 2010-04-04
I need to store more than one integer in TStringList.Objects
but couldn't find a way.

I read data from database, fill then into ListBox

for i:=0 to rst.RecordCount-1 do
  ListBox1.Items.AddObject(rst.FieldByName('Name').AsString,TObject(rst.FieldByName('Record_id').AsInteger);

I need to store one more integer value in TObject.
0
Comment
Question by:bogiboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 200 total points
ID: 8098932
Hi,

Try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMultiInteger = class
    Value1: Integer;
    Value2: Integer;
  end;

  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Count: Integer;
  MultiInteger: TMultiInteger;
begin
  for Count := 0 to 10 do
    begin
      MultiInteger := TMultiInteger.Create;
      MultiInteger.Value1 := Random(10);
      MultiInteger.Value2 := Random(10);
      Listbox1.Items.AddObject(IntToStr(Count), MultiInteger);
    end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
  MultiInteger: TMultiInteger;
begin
  if ListBox1.ItemIndex = -1 then
    Exit;
  MultiInteger := TMultiInteger(ListBox1.Items.Objects[ListBox1.ItemIndex]);
  ShowMessage(Format('Values :%d, %d', [MultiInteger.Value1, MultiInteger.Value2]));
end;

end.


Just put a TListBox on a form and link it's OnClick event to the one define here, and make sure the Form's OnCreate is linked to this one.

Cheers,


Stuart.
0
 
LVL 3

Expert Comment

by:ILE
ID: 8099029

u always cen put integer in to the string with


inttostr(i)      i is integer and result is string;

and get integer from string with

strtoint(s)    s is string wich contain integer and result is integer

0
 
LVL 3

Expert Comment

by:sfock
ID: 8099049
stuart's suggestion is okay, but don't forget to free the TMultiinteger instances when deleting itms or clearing or freeing the Listbox, otherwise you'll create some (hull) memory leaks ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:bogiboy
ID: 8099272
What about using record of two int values? Should I free objects in that case?
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8099312
No, you don't have to free it using records.  Using a class is a much neater way of doing it (and seeing as though Delphi is object driven, why not keep it that way?).

I was under the impression that those objects would be automatically freed by the ListBox's own destructor method.  If that's not the case, then just do this:

procedure TForm1.FormDestroy(Sender: TObject);
var
  Count: Integer;
begin
  for Count := pred(ListBox1.Items.Count) downto 0 do
    TMultiInteger(ListBox1.Items.Objects[Count]).Free;    
end;

Cheers,

Stuart.
0
 
LVL 1

Author Comment

by:bogiboy
ID: 8099323
What about using record of two int values? Should I free objects in that case?
0
 
LVL 1

Author Comment

by:bogiboy
ID: 8099363
Sorry for reposting.

Yes, Stuart, I want them to be released automatically.

Here is what I want to do:

1. Read records from dataset and put that in my StringList (as I explained);

2. Now, having that StringList as a property of my class, I want to simply call MyListBox.Items.Assign(myClass.MyStringList) to fire up simple dialogbox that will let user select value (I then want to read my integers from the Objects property of the MyListBox, not from myClass.myStringList)

So does that mean that I will have to free objects even if they are copied to MyDialog.MyListBox.Items.Objects and I call MyDialog.Free (i.e. I create form on the fly and destroy it after).
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8099381
Do you mean you have something like this:

type
  TMyValues = record
    Value1: Integer;
    Value2: Integer;
  end;


If so, you still won't be able to save them into a listbox.  The Object property needs a class or a pointer.  You'll have to do it like this:

type
  PMyValues = ^TMyValues;
  TMyValues = record
    Value1: Integer;
    Value2: Integer;
  end;


Then, when you go to add each item to the list, you'll have to do this:

var
  MyValue: PMyValues;
  Count: Integer;
begin
  for Count := 0 to 10 do
    begin
      New(MyValue);
      MyValue.Value1 := Random(10);
      MyValue.Value2 := Random(20);
      ListBox1.Items.AddObject(IntToStr(Count), Pointer(MyValue));
    end;
end;

To access the object, you have to do this:

var
  MyValue: PMyValues;
begin
  if ListBox1.ItemIndex = -1 then
    Exit;
  MyValue := PMyValues(ListBox1.Items.Objects[ListBox1.ItemIndex]);
  ShowMEssage(format('Values: %d, %d', [MyValue.Value1, MyValue.Value2]));
end;

And yes, you do have to free each item:

var
  Count: Integer;
begin
  for Count := pred(ListBox1.Items.Count) to 0 do
    Dispose(PMyValues(ListBox1.Items.Objects[Count]));
end;

(I have not compiled this code, it's just typed into the browser, so you may have some simple fixes to make to get it to compile).

I personally wouldn't use a record.  It's old.  Use the class method I posted first.

Stuart.
0
 
LVL 1

Author Comment

by:bogiboy
ID: 8099417
Thanks Stuart for being so quick. I am going to give it a try.
 I will try with objects instead of records. I feel safer than playing with pointers (coming from VB- no pointers there)
0
 
LVL 1

Author Comment

by:bogiboy
ID: 8099463
Thanks Stuart
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8099473
Have a play with the classes first.  It seriously is a much better way of doing it.  It's also a lot more flexible, plus  you can add properties to the class which can then trigger other events to do further processing - something you can't do in a record.

It's better to learn it right now than work through it using an old way and then realise later it would have been better to do it the right way first.

The choice is yours, but at least learn how to do it with classes.

Stuart.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8099474
Thanks for the grading.  Just holler if you need help.

Stuart.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question