An array of an array of records question...

Hi

I have the following question..
Am I doing this right? The point is I want to make a twodimensional array, which is in fact an array of another array type. This is what I do:

type FoodRec = record
  FoodID : Integer;
  FoodDescr : String[50];
  FoodPortion : String[50];
  FoodKCal : SmallInt;
  FoodFat : SmallInt;
  FoodQuantity : SmallInt;
  FoodRecNo : SmallInt;
end;

type
  MealFoods = Array of FoodRec;

var TotArray : Array of MealFoods;

then I can give it 'room' with setlength(TotArray[4][10]), in which 10 the dimension is of the Mealfoods array (I think..)
Is this the right way to do this?  Or is there a more simple or safer way..

Thanks..

Regards
lizzzardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheNeilCommented:
Well it compiles but I'd suggest that you actually define the sizes of your arrays in your type definitions. In your definition the 10 refers to the number of records while the 4 refers to the number of MealFood's. I've never seen SetLength used like that but it compiles and delphi seems happy enough with it but I'd still actually define the dimensions when you crate your types(go on, someone tell me I'm talking rubbish)

The Neil
0
VendiCommented:
I have used SetLength with dynamic arrays but typically I've done it as follows:

var TotArray: array of array of FoodRec;
..
..
SetLength(TotArray, I, J);
{gives I x J array)

or...

SetLength(TotArray, I);  Now you have an array of length I (I rows) whose elements you can set to arrays of varying length.  Like  SetLength(TotArray[1],5);

Also, it's pretty nice to use High, Low & Length functions with the dynamic arrays.

0
lizzzardAuthor Commented:
Hi,

Thanks for your comments..

The Neil, I feet the same way as you do, the type definition might get me into trouble. Vendi's solution is I think the one I'm gonna use..

Thanks again,

Lizzzard.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

OpitzSCommented:
Why don't you use
var
  totarray: array of array of MealFoods;

That is in my oppinion more readable.

Sven
0
dvalineCommented:
frankly,
  If you find this solution helpful email me at dvaline @ yahoo.com.  And let me at the points. LOL.

  I would create these records as TObject descendants.  Adding as a property to the first level of records a property which is descended off of TList.  This way you can use the power of OO and you can do validation and such as a contained business object.

Example.
  TMyFirstItem = class(TObject)
  private
    fIntItem: Integer;
    fStrItem: String;
    fSecondList: TMySecondList;
  public
    constructor Create;
      //  create the list object
    destructor Destroy; override;
      //  free the list object
    property StringItem: String read fStrItem write fStrItem;
    property IntItem: Integer read fIntItem write fIntItem;
    property SecondList: TMySecondList read fSecondList;
  end;

  TMyFirstList = Class(TList)
  private
    procedure SetItem(Index: Integer; Value: TMyFirstItem);
    function GetItem(Index: Integer): TMyFirstItem;
  public
    function Add: TMyFirstItem;
    procedure Delete(Index: Integer);
      // make sure to free your objects
    procedure Clear;
    property Items[Index: Integer]: TMyFirstItem read GetItem write SetItem;
  end;

  //  you would duplicate this kind of  
  //  declaration for your second item
  //  and list.
0
niknoxeCommented:
Take a look at this

{$R-}

type
  PFoodRec = ^FoodRec;
  FoodRec = record ... end;
type
  MealFoods = array [0..0] of PFoodRec;
 

begin
  MealFoods = new (SizeOf (FoodRec)*WantedItems));

for i:= 1 to WnatedItems-1 do begin
  with MealFoods [i]^ do begin
    ...
  end;
end;

This is not OO, o.k. But it works. You can nest them, if you want. It this is not the thing you want, start building objects as described.
0
VendiCommented:
I have used SetLength with dynamic arrays but typically I've done it as follows:

var TotArray: array of array of FoodRec;
...
...
SetLength(TotArray, I, J);
{gives I x J array)

or...

SetLength(TotArray, I);  Now you have an array of length I (I rows) whose elements you can set to arrays of varying length.  Like  SetLength(TotArray[1],5);

Also, it's pretty nice to use High, Low & Length functions with the dynamic arrays.

** since you said this is the one you would probably use earlier, here it is as an answer **
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lizzzardAuthor Commented:
OK,

Thanks Vendi.. Sorry, I should have asked you myself...

Regards,

Lizzzard
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.