From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8d182ecfa9220b7d X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Global Varibles!! Date: 1997/02/20 Message-ID: #1/1 X-Deja-AN: 220092868 References: <330B6914.22CD@hotmail.com> Content-Type: text/plain; charset=ISO-8859-1 Organization: Estormza Software Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1997-02-20T00:00:00+00:00 List-Id: In article <330B6914.22CD@hotmail.com>, rp71@hotmail.com wrote: I've jsut written a procedure which sets a value to a varible, and I >can't seen to keep the value for use in another procedure. I know it >has to be global but I don't know how to set a variable as global in >ada95. > >Can anyone please help me by telling me how to set a varible as global. I will tell you want to do, but with a warning: you must be careful with global variables. If you wish to save the state of a variable across subprogram invokations, then the variable must be declared in a static scope, which means in a package spec or body. For example, package Global is X : Integer; end; with Global; procedure Set_X is begin Global.X := 1; end; with Global; procedure Get_X is begin ... := Global.X; end; However, using a package to declare global variables shared by subprograms is a very old style of software architecture. You see it when Fortran programmers first program in Ada: they have a global mindset. Realize that this creates danderous coupling amoung pieces of the system. If you change Global.X, then how do you know what will break because of the change? Better is to encapsulate the data, and expose its representation to only a few subprograms. This is better known as object-oriented programming: the data is described not in terms of its representation, but rather in terms of the things you can do to it. For example, The_Stack : Stack; begin Push (5, On => The_Stack); ... The_Value := Top (The_Stack); Pop (The_Stack); The representation of the stack is hidden, and clients call primitive operations to manipulate stack instances. Your original problem was needing a static scope for global variables. While it's true we don't generally use global data anymore, we still have global entities in the guise of "well-known" objects. These are objects that are known across subsystem boundaries. We want to minimize coupling across subsystem boundaries, but of course we can't completely eliminate it. Subsystems have to have some minimum amount of coupling; otherwise, they'd wouldn't be part of the same system. For more information about this important topic, see visit the Singleton pattern, at the nascent SIGAda Patterns Working Group home page. -matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271