Having read up on variable declaration and making it global I though I had grasped it, but no, I have not. I have read a number of posts with similar issue but have not found solutions so I need a hint as to what I have missed.
I have declared my global variable gChartIndex in the interface section of my main unit FcMain. That should make it global and visible in other units.
unit FcMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
ExtCtrls, GroupsAndCharts, GetFileVerInfo, Printers,
Drawing, ChartSupport, ClsChartClasses;
Var
gChartIndex: Integer; //Index of selected chart in ChartListView
type
{ TfrmMain }
TfrmMain = class(TForm)
Button1: TButton;
//Lots of definitions deleted from here from brevity
procedure OptGrpAllClick(Sender: TObject);
procedure OptGrpSelClick(Sender: TObject);
private
public
end;
However in my Results unit I reference gChartIndex in procedure TfrmResults.cmdExitClick. I added FcMain in the Implementation section to avoid a circular reference. On running the value of gChartIndex is zero. I also tried to prefix with unit name ie. as FcMain.gChartIndex but still the value is zero. In both code chunks I have omitted procedures that are not involved to keep the code as short as possible. Help please.
unit Results;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, CheckLst,
ComCtrls, ExtCtrls;
type
{ frmResults }
{ TfrmResults }
TfrmResults = class(TForm)
cmdPrint: TButton;
cmdExit: TButton;
cmdResPrint: TButton;
cmdResExit: TButton;
Edit1: TEdit;
Edit2: TEdit;
FramePrint: TGroupBox;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
ListView1: TListView;
ListViewResults: TCheckListBox;
OptPrintAll: TRadioButton;
OptPrintRange: TRadioButton;
OptPrintSel: TRadioButton;
procedure cmdExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
private
public
end;
var
frmResults: TfrmResults ;
strVisiblePage: String; //The visible page in results list
gResCheck: Boolean; //Set when resultls listview checkbox set/unset
gResIndex: Integer; //The index of selected item in results ListView
implementation
{$R *.lfm}
Uses Drawing, FcMain ;
{ TfrmResults }
procedure TfrmResults.FormCreate(Sender: TObject);
begin
end;
procedure TfrmResults.cmdExitClick(Sender: TObject);
begin
//Close open charts
frmMain.GroupBox3.Visible:=True;
frmMain.ListViewCharts.SetFocus;
showMessage(gChartIndex.tostring);
frmMain.ListViewCharts.ItemIndex:=gChartIndex;
frmResults.Close;
end;