Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Code Problem

Posted on 2001-09-17
7
Medium Priority
?
154 Views
Last Modified: 2010-04-06
howdy...

could someone please inspect the following code, and tell me why the program just hangs on the line "Categories[0].Items[0].Create;"?

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].Items, 6);
  SetLength(Categories[1].Items, 6);
  SetLength(Categories[2].Items, 6);
  SetLength(Categories[3].Items, 6);
  SetLength(Categories[4].Items, 6);
  SetLength(Categories[5].Items, 6);

  Categories[0].Items[0].Create;
  Categories[0].Items[0].Add('HEH!');

  Categories[2].Items[5].Create;
  Categories[2].Items[5].Add('wei');
  Categories[2].Items[5].Add('diddy');

  Showmessage('Category 0, Item 0 is ' + Categories[0].Items[0].Strings[0]);
  Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Strings[0]);
  Showmessage('Category 2, Item 5 is ' + Categories[0].Items[2].Strings[1]);

{  try
    for Count1 := 0 to High(Categories) do
      for Count2 := 0 to High(Categories[Count1].Items) do
        if Assigned(Categories[Count1].Items[Count2]) then
          for Count3 := 0 to Categories[Count1].Items[Count2].Count do
            if Categories[Count1].Items[Count2].Strings[Count3] <> '' then
              Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[Count2].Strings[Count3]);
  except
    showmessage('we died');
  end;
}
end;

end.
0
Comment
Question by:X_Kalibur
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Author Comment

by:X_Kalibur
ID: 6486896
note that the second array of TStringList needs to be a dynamic resizable array...

the values of 6 for the size of the arrays were just something i set for testing purposes.
0
 
LVL 17

Accepted Solution

by:
geobul earned 800 total points
ID: 6486907
Hi,
I think:

Categories[0].Items[0] := TStringList.Create;

Regards, Geo
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6486928
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].Strings[0]);

so change it to
 Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Strings[0]);
 Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Strings[1]);


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6487005
also  change this line
for Count3 := 0 to Categories[Count1].Items[Count2].Count
to
for Count3 := 0 to Categories[Count1].Items[Count2].Count -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].Items, 6);
 SetLength(Categories[1].Items, 6);
 SetLength(Categories[2].Items, 6);
 SetLength(Categories[3].Items, 6);
 SetLength(Categories[4].Items, 6);
 SetLength(Categories[5].Items, 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].Strings[0]);
 Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Strings[0]);
 Showmessage('Category 2, Item 5 is ' + Categories[2].Items[5].Strings[1]);

 try
   for Count1 := 0 to High(Categories) do
     for Count2 := 0 to High(Categories[Count1].Items) do
       if Assigned(Categories[Count1].Items[Count2]) then
         for Count3 := 0 to Categories[Count1].Items[Count2].Count -1 do
           if Categories[Count1].Items[Count2].Strings[Count3] <> '' then
             Showmessage('Category ' + IntToStr(Count1) + ', Item ' + IntToStr(Count2) + ' is ' + Categories[Count1].Items[Count2].Strings[Count3]);
 except
   showmessage('we died');
 end;

end;
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6487188
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].Create;

Why do you need as many TStringList's for anyways?
0
 
LVL 1

Author Comment

by:X_Kalibur
ID: 6489270
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!
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6489399
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 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