Recent

Author Topic: [SOLVED] Vector of Record  (Read 734 times)

Slyde

  • Full Member
  • ***
  • Posts: 152
[SOLVED] Vector of Record
« on: July 20, 2022, 08:58:39 pm »
I can't figure out to access the record variables once I store them in a vector.  For example, I store three integer variables in a record:

Code: Pascal  [Select][+][-]
  1. TmyRecord = record
  2.     id       : Integer;
  3.     x        : integer;
  4.     y        : integer;
  5.   end;
  6.  
  7. var
  8.   myRecord : TmyRecord
  9.  
  10. { store integers in the three vars here }

Then I flush the record to the to a vector:

Code: Pascal  [Select][+][-]
  1. iVec.PushBack(myRecord);

So far, so good.  But for the life of me, I can't puzzle out how I'm suppose to retrieve that information.

I've tried stuff like this:

Code: Pascal  [Select][+][-]
  1. Query1.Params.ParamByName('id').AsInteger:= iVec[i].myRecord.id;  // i is a counter

...which obviously doesn't work.  The thing is, I haven't found anything online abt this.  Which makes me think that, while I'm able to store a record inside a vector, calling the variables individually from the vector isn't possible.  So there's no need to discuss it, hence the complete lack of online content on it. 

I can't figure out why I'm able to store a record in a vector and not be able to individually access the record variables.  Is it even possible?
« Last Edit: July 20, 2022, 09:49:18 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 17388
  • Ceterum censeo Trump esse delendam
Re: Vector of Record
« Reply #1 on: July 20, 2022, 09:06:22 pm »
In mode objfpc use ^ to dereference the stored pointer, or use mode delphi.
iVec^.myRecord.id maybe?
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

ASerge

  • Hero Member
  • *****
  • Posts: 2436
Re: Vector of Record
« Reply #2 on: July 20, 2022, 09:32:23 pm »
Code: Pascal  [Select][+][-]
  1. Query1.Params.ParamByName('id').AsInteger:= iVec[i].myRecord.id;  // i is a counter
".myRecord" is superfluous.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses gvector;
  5.  
  6. type
  7.   TMyRecord = record
  8.     Id: Integer;
  9.     X: Integer;
  10.     Y: Integer;
  11.   end;
  12.   TMyVector = specialize TVector<TMyRecord>;
  13.  
  14. var
  15.   R: TMyRecord = (Id:1; X:2; Y:3);
  16.   V: TMyVector;
  17. begin
  18.   V := TMyVector.Create;
  19.   try
  20.     V.PushBack(R);
  21.     Writeln(V[0].Y);
  22.   finally
  23.     V.Free;
  24.   end;
  25.   Readln;
  26. end.

Warfley

  • Hero Member
  • *****
  • Posts: 1929
Re: Vector of Record
« Reply #3 on: July 20, 2022, 09:32:36 pm »
Why do you have .myRecord in there? Just use iVec.id

Example:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   gvector;
  7.  
  8. type
  9.   TMyRec = record
  10.     A, B: Integer;
  11.   end;
  12.   TMyVec = specialize TVector<TMyRec>;
  13.  
  14. var
  15.   Vec: TMyVec;
  16.   Rec: TMyRec;
  17. begin
  18.   Rec.A:=42;
  19.   Rec.B:=32;
  20.   Vec := TMyVec.Create;
  21.   try
  22.     Vec.PushBack(Rec);
  23.     WriteLn(Vec[0].A);
  24.   finally
  25.     Vec.Free;
  26.   end;
  27. end.
  28.  

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: Vector of Record
« Reply #4 on: July 20, 2022, 09:48:54 pm »
I hope that I can one day be as good as the ones who answered this.  Truth is, I'd rather slide down a pole of razor blades into a pool of alcohol than to have to rely on anyone for help.  But it's difficult (for me) to always be able to find resources on how to do some of the stuff (like this) that I want to incorporate in my application.  I appreciate all your help.  Thanks.
« Last Edit: July 21, 2022, 04:30:19 am by Slyde »
Linux Mint 21.3
Lazarus 3.0

 

TinyPortal © 2005-2018