bogiboy
asked on
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(r st.FieldBy Name('Name ').AsStrin g,TObject( rst.FieldB yName('Rec ord_id').A sInteger);
I need to store one more integer value in TObject.
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(r
I need to store one more integer value in TObject.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;-)
ASKER
What about using record of two int values? Should I free objects in that case?
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.Ite ms.Objects [Count]).F ree;
end;
Cheers,
Stuart.
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:
var
Count: Integer;
begin
for Count := pred(ListBox1.Items.Count)
TMultiInteger(ListBox1.Ite
end;
Cheers,
Stuart.
ASKER
What about using record of two int values? Should I free objects in that case?
ASKER
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(myC lass.MyStr ingList) 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.O bjects and I call MyDialog.Free (i.e. I create form on the fly and destroy it after).
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(myC
So does that mean that I will have to free objects even if they are copied to MyDialog.MyListBox.Items.O
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(I ntToStr(Co unt), 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.O bjects[Lis tBox1.Item Index]);
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.Obj ects[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.
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(I
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.O
ShowMEssage(format('Values
end;
And yes, you do have to free each item:
var
Count: Integer;
begin
for Count := pred(ListBox1.Items.Count)
Dispose(PMyValues(ListBox1
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.
ASKER
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)
I will try with objects instead of records. I feel safer than playing with pointers (coming from VB- no pointers there)
ASKER
Thanks Stuart
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.
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.
Thanks for the grading. Just holler if you need help.
Stuart.
Stuart.
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