?
Solved

dynamic array of records in dynamic array of records!!

Posted on 2003-11-20
8
Medium Priority
?
452 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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