Solved

dynamic array of records in dynamic array of records!!

Posted on 2003-11-20
8
450 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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