Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

How to store 2 int int TStrings?

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
bogiboy
Asked:
bogiboy
1 Solution
 
Stuart_JohnsonCommented:
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
 
ILECommented:

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
 
sfockCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bogiboyAuthor Commented:
What about using record of two int values? Should I free objects in that case?
0
 
Stuart_JohnsonCommented:
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
 
bogiboyAuthor Commented:
What about using record of two int values? Should I free objects in that case?
0
 
bogiboyAuthor Commented:
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
 
Stuart_JohnsonCommented:
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
 
bogiboyAuthor Commented:
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
 
bogiboyAuthor Commented:
Thanks Stuart
0
 
Stuart_JohnsonCommented:
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
 
Stuart_JohnsonCommented:
Thanks for the grading.  Just holler if you need help.

Stuart.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now