[Resolved] Global variable oddities lazarus / 21-General ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 17:37 Greetings everyone,  I have done a forum search on this topic, and I have done everything suggested, but I'm still not getting the results I expect.  I am declaring TestString in appstuff.pas, then including it in the 'uses' clause in both unit1.pas and unit2.pas, but when I attempt to show the value of TestString in unit2.pas, it's empty. Let me show the relevant code: appstuff.pas 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 AppStuff; {$mode objfpc}{$H+} interface uses  Classes, SysUtils; var  TestString: String; implementation end.  unit1.pas 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  AppStuff, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,  ActnList, Buttons, Unit2; ... procedure TForm1.FormCreate(Sender: TObject);begin  AppStuff.TestString := 'test';end;  Clicking a button on the unit1.pas form displays the unit2.pas form, then... unit2.pas 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 Unit2; {$mode objfpc}{$H+} interface uses  AppStuff, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,  ComCtrls; ...  procedure TForm2.Button1Click(Sender: TObject);begin  ShowMessage(AppStuff.TestString);  // This shows an empty stringend;  Am I missing some basic rule here?  When I made a separate 'proof of concept' project just to test global variables, it works, so I'm a little baffled.  I also tried putting 'AppStuff' at the end of the 'uses' clause and it made no difference. Thank you! Will B ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 18:02 Curiously, if I do this in unit1.pas, the ShowMessage call in unit2.pas displays 'test3'.  What in the world is going on here??? unit1.pas 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 procedure TForm1.FormShow(Sender: TObject);begin  AppStuff.TestString := 'test3';end;  Is this a bug in FormCreate? ---------------------------------------------------------------------- ccrause · 2021-11-04 18:10 Quote from: willbprog9933 on November 04, 2021, 07:02:04 pmCuriously, if I do this in unit1.pas, the ShowMessage call in unit2.pas displays 'test3'.  What in the world is going on here??? Perhaps a stale unit was linked in?  Build your project (or shift + F9) or even better clean and rebuild to force a recompile of all units in your project to prevent this. ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 18:15 Quote from: ccrause on November 04, 2021, 07:10:11 pmPerhaps a stale unit was linked in?  Build your project (or shift + F9) or even better clean and rebuild to force a recompile of all units in your project to prevent this. Great idea!  I tried it based on your suggestion, but the issue is still happening.  It's really baffling, because FormCreate works fine in the stripped down 'proof of concept' project I did. ---------------------------------------------------------------------- skalogryz · 2021-11-04 18:28 Quote from: willbprog9933 on November 04, 2021, 07:15:40 pmIt's really baffling, because FormCreate works fine in the stripped down 'proof of concept' project I did.  are you sure that no other code clears up AppStuff.TestString? could it be that there's TForm2.FormCreate that does it? ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 18:31 Quote from: skalogryz on November 04, 2021, 07:28:16 pmare you sure that no other code clears up AppStuff.TestString? could it be that there's TForm2.FormCreate that does it? I'm absolutely positive that nothing else modifies AppStuff.TestString.  I just double-checked.  There is nothing at all in Form2.FormCreate that touches AppStuff.TestString.  AppStuff.TestString was added for debugging purposes and isn't part of the program proper, so it's only being used sparingly. Thanks Will B ---------------------------------------------------------------------- skalogryz · 2021-11-04 18:34 ok. let's debug the debugging purposes then! Try to modify your AppStuff unit. Instead of having "var TestString" let's make it a property: 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 function GetAppString: string;procedure SetAppString(const v: string); property TestString: string read GetAppString write SetAppString; implementation {$R *.lfm} var  _AppString : string; function GetAppString: string;begin  Result := _AppString;end; procedure SetAppString(const v: string);begin  writeln('value changed to: "',v,'"');  _AppString := v;end; recompile your project and if you're on Windows don't forget to mark it as non-Win32 GUI application. if you are on one of the unix platforms you need to launch the app from command-line ---------------------------------------------------------------------- WooBean · 2021-11-04 18:49 @Will B Are you shure that TForm1.FormCreate(Sender: TObject) is assigned to OnCreate event for form1? Just set a breakpoint inside FormCreate code body and press F9 key. ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 18:52 Quote from: skalogryz on November 04, 2021, 07:34:04 pmok. let's debug the debugging purposes then! Try to modify your AppStuff unit. Instead of having "var TestString" let's make it a property: Thanks for that! I did that (had to remove the {$R...} directive because appstuff.pas is a unit).  The only time it logs to the console is when I assign in Form1.FormShow, it does *not* log when I try to assign in Form1.FormCreate.  Ugh.  For some strange reason, it's completely skipping the .FormCreate event on Form1/Unit1! ---------------------------------------------------------------------- willbprog9933 · 2021-11-04 18:55 Quote from: WooBean on November 04, 2021, 07:49:57 pm@Will B Are you shure that TForm1.FormCreate(Sender: TObject) is assigned to OnCreate event for form1? Excellent question!  That's what the problem was!  I feel so dumb.  I don't know how that got unassigned to the FormCreate event, but after reassigning it, it's working fine now. Thank you all for the excellent suggestions!  I'm so sorry for the noise! Will B.