[SOLVED] How to set a global TVector and access it lazarus / 21-General ---------------------------------------------------------------------- Slyde · 2022-06-19 23:46 I'm rewriting a C++ program that relies on a few vectors.  My issue is that I can't figure out how to create a couple of them for global use.  Showing the relevant code, this is what doesn't work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Classes, SysUtils, gvector; type   { TForm1 }   TForm1 = class(TForm)  procedure cboxSelectNameChange(Sender: TObject);      ...   private   public    procedure Test1;    procedure Test2;   end; var  Form1: TForm1; implementation uses  IniFiles,  Unit2,  dMod,  LCLType; type  iVector = specialize TVector; var shuffleVec: iVector; {$R *.lfm} procedure TForm1.cboxSelectNameChange(Sender: TObject);begin  lblStudierID.Caption:= cboxSelectName.Text;  Test1;  Test2;end; procedure TForm1.Test1;var  i: integer;begin  //shuffleVec.Create;  for i:= 0 to 4 do shuffleVec.PushBack(i);end; procedure TForm1.Test2;begin  Label2.Caption:= IntToStr(shuffleVec[2]);  // should show '1' in the label, but the label never changesend;  I've searched here and online and found no useful information.  99.9% of the searches showed imaging results.  How do I create and use global vectors in FP? ---------------------------------------------------------------------- HeavyUser · 2022-06-20 00:21 Declare them in the interface section of the unit, not the implementation section. ---------------------------------------------------------------------- jamie · 2022-06-20 00:25 I see that you are not creating the vector? You have it commented out.  you need to create it at some point before using it.  You can use the "OnCreate" event of the form to do that, create the vector there and move the type up before the TFORM so the form can also see it.  Remember to free this vector at some point to avoid memory leaks. the OnDestroy of the form can be used for that. ---------------------------------------------------------------------- bytebites · 2022-06-20 02:02 Create it correctly Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i shuffleVec:=iVector.Create; ---------------------------------------------------------------------- Slyde · 2022-06-20 05:02 After fumbling around like the old codger I am, I finally got it  Thank you for your explanation, jamie.  It was more than enough...and right on time. Here's the proof   Thanks again. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, gvector; type  iVector = specialize TVector;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private   public    procedure TestVec;   end; var  Form1: TForm1;  shuffleVec: iVector; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  shuffleVec:= iVector.Create;end; procedure TForm1.Button1Click(Sender: TObject);var i: Integer;begin  for i:= 1 to 5 do ShuffleVec.PushBack(i);  TestVec;end; procedure TForm1.FormDestroy(Sender: TObject);begin  shuffleVec.Free;end; procedure TForm1.TestVec;begin  Label1.Caption:= IntToStr(shuffleVec[2]);end; end. ---------------------------------------------------------------------- PascalDragon · 2022-06-20 11:39 Quote from: Slyde on June 20, 2022, 07:02:05 amAfter fumbling around like the old codger I am, I finally got it  Thank you for your explanation, jamie.  It was more than enough...and right on time. Here's the proof   Thanks again. Since shuffleVec is a global variable you should create it in the initialization-section of the unit and free it in the finalization- section cause otherwise there would be problems if you create the form multiple times. Also shuffleVec does not need to be part of the interface-section if you don't need it to be useable outside of the unit. ---------------------------------------------------------------------- Slyde · 2022-06-20 14:32 Quote from: PascalDragon on June 20, 2022, 01:39:27 pmSince shuffleVec is a global variable you should create it in the initialization- section of the unit and free it in the finalization-section cause otherwise there would be problems if you create the form multiple times. Also shuffleVec does not need to be part of the interface- section if you don't need it to be useable outside of the unit. I appreciate the insight, PascalDragon.  Thank you.