X_Kalibur
asked on
Code Problem
howdy...
could someone please inspect the following code, and tell me why the program just hangs on the line "Categories[0].Items[0].Cr eate;"?
maybe im doing something wrong, but im not extirely sure if what im doing is correct way either...
Basically i need to have an array [0..5] each element containing an array of TStrings.
if there is a better way of doing that, i'd apprecaite being filled in! :P
Thanks
X
-----------------------
unit mainform;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TCategoryItems = record
Items: array of TStringList;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Categories: array[0..5] of TCategoryItems;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses StringRoutines;
{$R *.DFM}
procedure TForm1.Button1Click(Sender : TObject);
var
Count1, Count2, Count3 : integer;
begin
SetLength(Categories[0].It ems, 6);
SetLength(Categories[1].It ems, 6);
SetLength(Categories[2].It ems, 6);
SetLength(Categories[3].It ems, 6);
SetLength(Categories[4].It ems, 6);
SetLength(Categories[5].It ems, 6);
Categories[0].Items[0].Cre ate;
Categories[0].Items[0].Add ('HEH!');
Categories[2].Items[5].Cre ate;
Categories[2].Items[5].Add ('wei');
Categories[2].Items[5].Add ('diddy');
Showmessage('Category 0, Item 0 is ' + Categories[0].Items[0].Str ings[0]);
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str ings[0]);
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str ings[1]);
{ try
for Count1 := 0 to High(Categories) do
for Count2 := 0 to High(Categories[Count1].It ems) do
if Assigned(Categories[Count1 ].Items[Co unt2]) then
for Count3 := 0 to Categories[Count1].Items[C ount2].Cou nt do
if Categories[Count1].Items[C ount2].Str ings[Count 3] <> '' then
Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[C ount2].Str ings[Count 3]);
except
showmessage('we died');
end;
}
end;
end.
could someone please inspect the following code, and tell me why the program just hangs on the line "Categories[0].Items[0].Cr
maybe im doing something wrong, but im not extirely sure if what im doing is correct way either...
Basically i need to have an array [0..5] each element containing an array of TStrings.
if there is a better way of doing that, i'd apprecaite being filled in! :P
Thanks
X
-----------------------
unit mainform;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TCategoryItems = record
Items: array of TStringList;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Categories: array[0..5] of TCategoryItems;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses StringRoutines;
{$R *.DFM}
procedure TForm1.Button1Click(Sender
var
Count1, Count2, Count3 : integer;
begin
SetLength(Categories[0].It
SetLength(Categories[1].It
SetLength(Categories[2].It
SetLength(Categories[3].It
SetLength(Categories[4].It
SetLength(Categories[5].It
Categories[0].Items[0].Cre
Categories[0].Items[0].Add
Categories[2].Items[5].Cre
Categories[2].Items[5].Add
Categories[2].Items[5].Add
Showmessage('Category 0, Item 0 is ' + Categories[0].Items[0].Str
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str
{ try
for Count1 := 0 to High(Categories) do
for Count2 := 0 to High(Categories[Count1].It
if Assigned(Categories[Count1
for Count3 := 0 to Categories[Count1].Items[C
if Categories[Count1].Items[C
Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[C
except
showmessage('we died');
end;
}
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello
You need to call create method for each Categories[?].items[?] before you can use it
and items is TStringList, so you need to create it as Geo suggested to you
also you try to access items not added in some Categories
you added items to Categories[2].Items[5]
Categories[2].Items[5].Add ('wei');
Categories[2].Items[5].Add ('diddy');
but u try to access to
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str ings[0]);
so change it to
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str ings[0]);
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str ings[1]);
You need to call create method for each Categories[?].items[?] before you can use it
and items is TStringList, so you need to create it as Geo suggested to you
also you try to access items not added in some Categories
you added items to Categories[2].Items[5]
Categories[2].Items[5].Add
Categories[2].Items[5].Add
but u try to access to
Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Str
so change it to
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str
also change this line
for Count3 := 0 to Categories[Count1].Items[C ount2].Cou nt
to
for Count3 := 0 to Categories[Count1].Items[C ount2].Cou nt -1 // count -1
ur code will look like this after the few modifications
procedure TForm1.Button1Click(Sender : TObject);
var
Count1, Count2, Count3 : integer;
begin
SetLength(Categories[0].It ems, 6);
SetLength(Categories[1].It ems, 6);
SetLength(Categories[2].It ems, 6);
SetLength(Categories[3].It ems, 6);
SetLength(Categories[4].It ems, 6);
SetLength(Categories[5].It ems, 6);
Categories[0].Items[0] :=TStringList.Create;
Categories[0].Items[0].Add ('HEH!');
Categories[2].Items[5] :=TStringList.Create;;
Categories[2].Items[5].Add ('wei');
Categories[2].Items[5].Add ('diddy');
Showmessage('Category 0, Item 0 is ' + Categories[0].Items[0].Str ings[0]);
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str ings[0]);
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str ings[1]);
try
for Count1 := 0 to High(Categories) do
for Count2 := 0 to High(Categories[Count1].It ems) do
if Assigned(Categories[Count1 ].Items[Co unt2]) then
for Count3 := 0 to Categories[Count1].Items[C ount2].Cou nt -1 do
if Categories[Count1].Items[C ount2].Str ings[Count 3] <> '' then
Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[C ount2].Str ings[Count 3]);
except
showmessage('we died');
end;
end;
for Count3 := 0 to Categories[Count1].Items[C
to
for Count3 := 0 to Categories[Count1].Items[C
ur code will look like this after the few modifications
procedure TForm1.Button1Click(Sender
var
Count1, Count2, Count3 : integer;
begin
SetLength(Categories[0].It
SetLength(Categories[1].It
SetLength(Categories[2].It
SetLength(Categories[3].It
SetLength(Categories[4].It
SetLength(Categories[5].It
Categories[0].Items[0] :=TStringList.Create;
Categories[0].Items[0].Add
Categories[2].Items[5] :=TStringList.Create;;
Categories[2].Items[5].Add
Categories[2].Items[5].Add
Showmessage('Category 0, Item 0 is ' + Categories[0].Items[0].Str
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str
Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Str
try
for Count1 := 0 to High(Categories) do
for Count2 := 0 to High(Categories[Count1].It
if Assigned(Categories[Count1
for Count3 := 0 to Categories[Count1].Items[C
if Categories[Count1].Items[C
Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[C
except
showmessage('we died');
end;
end;
You may also want to enable the range checks in the compiler options while developing such stuff, it will detect bounds errors in arrays.
Very important, create the TStringList's as shown by geobul:
Categories[0].Items[0] := TStringList.Create;
instead of
Categories[0].Items[0].Cre ate;
Why do you need as many TStringList's for anyways?
Very important, create the TStringList's as shown by geobul:
Categories[0].Items[0] := TStringList.Create;
instead of
Categories[0].Items[0].Cre
Why do you need as many TStringList's for anyways?
ASKER
Thanks!
You wouldnt belive it, but this was the second time ive done something stupid like that ever, and posted a question on EE, cause i overlooked it each time!!
Basically i need to maintain a list of categories which will be read in from a file...and each category will have a variable list of sub-categories - hence the array of dynamic array..
Thanks everyone for all your help!
You wouldnt belive it, but this was the second time ive done something stupid like that ever, and posted a question on EE, cause i overlooked it each time!!
Basically i need to maintain a list of categories which will be read in from a file...and each category will have a variable list of sub-categories - hence the array of dynamic array..
Thanks everyone for all your help!
I would have solved this by makinc a custom TStringList descendant which has another TStringList assigned to each string... this would allow to build a "tree" of TStringLists without the hassle of mixing arrays and lists.
ASKER
the values of 6 for the size of the arrays were just something i set for testing purposes.