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?
 
VendiConnect With a Mentor Commented:
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
 
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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
 
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
 
lizzzardAuthor Commented:
OK,

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

Regards,

Lizzzard
0
All Courses

From novice to tech pro — start learning today.