Recent

Author Topic: Sharing variables between forms  (Read 6350 times)

Neojag

  • New member
  • *
  • Posts: 8
Sharing variables between forms
« on: April 30, 2008, 12:27:38 am »
I need to share variables between forms, but can't really decide what's the best way to do it.
I've read around for methods of implementing global variables, and all I found was having a unit with the globals I wanted declared and including it in all the units I want the variables available to. This seems to be rather unelegant, as I was searching for more like a C style of doing it. Is it the only way? Also, is there a way of passing variables to forms upon their creation? Or, upon being show, if they are already created.
Thanks

cyber_python

  • Jr. Member
  • **
  • Posts: 79
Re: Sharing variables between forms
« Reply #1 on: April 30, 2008, 12:52:06 am »
Quote from: "Neojag"
Also, is there a way of passing variables to forms upon their creation? Or, upon being show, if they are already created.
Thanks

You can use the form's onCreate and onShow events.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8416
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Re: Sharing variables between forms
« Reply #2 on: April 30, 2008, 08:16:31 am »
Quote
This seems to be rather unelegant, as I was searching for more like a C style of doing it

How does 'C-style' do it?

Talking about elegance... why not create an interface with variables you want there, then make your form inherits it. Such as:
Code: [Select]

type

  IMyForm = interface
    MyVar: Integer;
  end;

  TMyForm = class(TForm,IMyForm)
  end;

Don't know whether it will work, but it's worth to try.

duncanparsons

  • Jr. Member
  • **
  • Posts: 83
RE: Re: Sharing variables between forms
« Reply #3 on: May 01, 2008, 10:43:50 am »
Interface wouldn't work, the variables would be local to the object still.

By 'C style', do you mean having a singleton-style variable that is a STRUCT with all your globals in some kind of header, and each file has an include of that header? If so, that's exactly what having a separate unit and including it in uses clause does.

If you want to be OO, rather than STRUCT/record based, you could have a global.pas, and define
Code: [Select]
type
  TGlobals=class
    //your globals go here
    //  either make everything public, like a glorified record,
    //  or use properties with proper accessors...

    public
      constructor create; //do default values here
      destructor destroy; override; //free any created resources
  end;

var Globals:TGlobals


then at the end of the implementation..
Code: [Select]

initialization
  Globals:=TGlobals.create;

finalization
  Globals.Free;
end.


You then have a Globals object which can be passed around. For extra security, you could put class functions in to reference count (or descend from TInterfacedObject and override the AddRef) and generate an exception if more than 1 object is created at a time, to ensure the singleton status. Of course, if it's your own code, and only you will manage it, then that might be overboard for what you need.

How very smalltalk, how very Eiffel ;)

I often have a unit with just consts and globals, or a file with each. I've never seen the point of making an object when I don't have to, there's the advantages and disadvantages, but overall, if there's just a value in memory, I would prefer direct access rather than through an accessor.

Be aware that your globals won't be thread safe. To ensure thread safety, then it would be worth making an object, and having properties with write methods which use critical sections. Examine your needs, and act accordingly :)

DSP

Neojag

  • New member
  • *
  • Posts: 8
RE: Re: Sharing variables between forms
« Reply #4 on: May 02, 2008, 12:11:11 am »
That was exactly it, many thanks ;)

eldonfsr

  • Sr. Member
  • ****
  • Posts: 283
Re: Sharing variables between forms
« Reply #5 on: March 01, 2022, 05:58:24 pm »
Sorry for that i trying to work with, i don't what i doimg wrong send me error on thi sline
  MyConnections=array[1..MaxConnections] of conectiondata; // fixed size   
 sad  fmain.pas(50,17) Fatal: Syntax error, ":" expected but "=" found

this my code
Code: Pascal  [Select][+][-]
  1. unit Fmain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, IdHTTPServer,
  9.   IdTCPServer, IdCustomTCPServer, IdContext, IdTCPClient, IdCmdTCPServer,
  10.   IdBaseComponent, IdComponent, IdCommandHandlers;
  11.  
  12. Type
  13.    conectiondata = Packed Record
  14.     // integer, string and boolean types
  15.     context: TIdContext;
  16.   end;
  17. type
  18.  
  19.   { TFServer }
  20.  
  21.  
  22.  
  23.   TFServer = class(TForm)
  24.     BtnStart: TButton;
  25.     BtnStop: TButton;
  26.     BtnClear: TButton;
  27.     IdCmdTCPServer1: TIdCmdTCPServer;
  28.     IdTCPServer1: TIdTCPServer;
  29.     clients_connected: TLabel;
  30.     messagesLog: TMemo;
  31.     procedure BtnStartClick(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure FormDestroy(Sender: TObject);
  34.     procedure FormShow(Sender: TObject);
  35.     procedure IdCmdTCPServer1CommandHandlers0Command(ASender: TIdCommand);
  36.     procedure IdTCPServer1Execute(AContext: TIdContext);
  37.     procedure IdTCPServer1Connect(AContext: TIdContext);
  38.     procedure addnewconnection(concontext:TIdContext);
  39.   private
  40.  
  41.  
  42.   public
  43. end;
  44.  
  45. const GUEST_CLIENT_PORT = 20010;
  46.       MaxConnections=20;
  47.  
  48. var
  49.    FServer: TFServer;
  50.    MyConnections=array[1..MaxConnections] of conectiondata; // fixed size
  51.  
  52. implementation
  53.     uses LazUtils, LazUTF8;
  54. {$R *.lfm}
  55.  
  56.  
  57. { TFServer }
  58.  
  59.  
  60.  
  61. procedure TFServer.IdTCPServer1Execute(AContext: TIdContext);
  62. var
  63.   recv:string;
  64. begin
  65.  //  recv := AContext.Connection.Socket.ReadLn;
  66.  
  67.    MessagesLog.Lines.Add ('Connected f r o m : ' +AContext.Connection.Socket.Binding.IP);
  68. end;
  69.  
  70. procedure TFServer.FormCreate(Sender: TObject);
  71. begin
  72.     IdTCPServer1.Active          := False;
  73.  
  74.     // ... set properties
  75.     IdTCPServer1.MaxConnections  := 20;
  76.  
  77. end;
  78.  
  79.  

Thaddy

  • Hero Member
  • *****
  • Posts: 11522
Re: Sharing variables between forms
« Reply #6 on: March 01, 2022, 06:46:08 pm »
Put the shared variables in a separate record. Even in a separate unit too.
Путин преступник. Россияне дезинформированы.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4188
Re: RE: Re: Sharing variables between forms
« Reply #7 on: March 01, 2022, 07:55:33 pm »
Quote
This seems to be rather unelegant, as I was searching for more like a C style of doing it
How does 'C-style' do it?

I agree: please tell us what you think you're talking about (and what you think Pascal can't do).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1980
  • Former Delphi 1-7, 10.2 user
Re: RE: Re: Sharing variables between forms
« Reply #8 on: March 01, 2022, 11:07:22 pm »
Quote
This seems to be rather unelegant, as I was searching for more like a C style of doing it
How does 'C-style' do it?

I agree: please tell us what you think you're talking about (and what you think Pascal can't do).

You're very optimistic... the OP has not logged in for 14 years :-)
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

Lansdowne

  • New Member
  • *
  • Posts: 15
Re: Sharing variables between forms
« Reply #9 on: March 02, 2022, 01:10:41 am »
Sorry for that i trying to work with, i don't what i doimg wrong send me error on thi sline
  MyConnections=array[1..MaxConnections] of conectiondata; // fixed size   
 sad  fmain.pas(50,17) Fatal: Syntax error, ":" expected but "=" found


In this case, the error message tells you exactly what your error is.
Syntax error, ":" expected but "=" found

When you define var, you need varname:type, not varname=type
MyConnections:array[     not Myconnections=array[

 

TinyPortal © 2005-2018