Solved

dynamic array of records in dynamic array of records!!

Posted on 2003-11-20
8
448 Views
Last Modified: 2010-04-05
Hey all,

I have a couple of records as follows :

type
 WEAPON = record
  size : byte;
  posX : double;
  posY : double;
  posZ : double;
  rotX : double;
  rotY : double;
  rotZ : double;
end;

type
 DATA = record
  LowestX : double;
  LowestY : double;
  HighestX : double;
  HighestY : double;
  weight : double;
  braking : double;
  power : double;
  hitpoints : integer;
  cost : integer;
  name : string;
  path : string;
  WeaponsNumber : byte;
  Weapons : array of WEAPON; //<---- right here!
end;

As u see, the DATA record has an array of WEAPON at the bottom.... ( as shown)

now i have a global dynamic array of DATA which works fine,

  Tanks : array of DATA;

and i can call stuff like

SetLength(Tanks, 100);
Tanks[0].weight = 1000;

but when i call SetLength on the WEAPONS record within the DATA one, an access violation occurs...
i.e.
SetLength(Tanks[0].Weapons, 100); //Access violation!!!

Please help!!!

Regards,
-Rob-
0
Comment
Question by:Excalibur81
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 50 total points
ID: 9795422
I tested the following code out and worked fine:

procedure TForm1.Button1Click(Sender: TObject);
var
Tanks : array of DATA;
begin
  SetLength(Tanks, 100);
  Tanks[0].weight := 1000;
  SetLength(Tanks[0].Weapons, 100);
  tanks[0].Weapons[0].size := 9;
  showmessage(inttostr(tanks[0].Weapons[0].size));
  tanks[0].Weapons[2].size := 54;
  showmessage(inttostr(tanks[0].Weapons[2].size));
end;

WinXP Pro / D7 Ent
0
 
LVL 7

Expert Comment

by:jconde
ID: 9795430
BTW, even though it would slightly add complexity to your code, I would suggest you use a TList for this instead of a dynamic array.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9795449
Hi,

No access violation occurs in my Delphi 5. Everything is just fine:

var
  Form1: TForm1;
  Tanks : array of DATA;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(Tanks, 100);
  Tanks[0].weight := 1000;
  SetLength(Tanks[0].Weapons, 100);
  Tanks[0].Weapons[0].PosX := 100.25;
  ShowMessage(FloatToStr(Tanks[0].Weapons[0].PosX));
end;

Regards, Geo
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 11

Expert Comment

by:ZhaawZ
ID: 9796061
btw, you could use length(tanks[n].weapons) instead of tanks[n].weaponnumber ;)
0
 

Author Comment

by:Excalibur81
ID: 9796252
Ahhh i see now, it was a logic mistake :)

Hmm, so since there was actually no problem, how do points work??


Also.....

"BTW, even though it would slightly add complexity to your code, I would suggest you use a TList for this instead of a dynamic array. "
Unfortunatly, im saving the data in a file which will be read later on by a program in C++, and needs to be in this fairly specific format.... thanks anyways :)

"btw, you could use length(tanks[n].weapons) instead of tanks[n].weaponnumber ;) "
Yes i had seen that, but for now im just using this for debugging purposes, but thanks also :)

0
 
LVL 7

Expert Comment

by:jconde
ID: 9800229
I beleive you should split the points between geobul and myself.  geobul, what do you suggest ?
0
 
LVL 17

Expert Comment

by:geobul
ID: 9802595
jconde should get the points. I just confirmed that for D5.
0
 

Author Comment

by:Excalibur81
ID: 9805327
Thanks guys :)

Soz, i meant to split the points, but ive never done before, and i thought it was inside the "Accept Answer" path....

but i guess geobul didnt want them anyways :D


Thanks!!!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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