Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Re: Passing Global Public variable to function


Message #1 by "=?iso-8859-1?B?QUpBWK4=?=" <ajax@i...> on Thu, 6 Jun 2002 09:47:32 +0530
Like to explain with an example :
consider a function that accepts a ConnectionString  as a param and returns
a ADODB connection
Now the connectionString is generally a global variable in the app, and is
accessible inside  the connection returning function, but by modifying its
signature to accept the string as a param, the function becomes reusable as
in I can pass a different connectionstring and connect to a different
database if needed.

Therefore the nature of the function is an important factor that will weigh
heavily in deciding the signature of any function, rather that the scoping
of the variable it uses within.

----- Original Message -----
From: "Randy Cornish" <rlcornish@c...>
To: "professional vb" <pro_vb@p...>
Sent: Friday, May 31, 2002 7:43 PM
Subject: [pro_vb] Re: Passing Global Public variable to function


> Jerry -
> I know a lot of time has passed since this posting, but thought I'd throw
> in a comment anyway.  Had some old postings that I never read until today.
>
> VB "best practices" frown on EVER using global variables.  The main
> reason is because the scope is so broad, that any code inside you
> app/component can change the value and it is hard to control.  The best
> practice is to scope everything at some level below global OR to use a
> global object with "Properties" to control who/how variables are altered.
>
> It is also best practices to pass in, as a parameter, all data that that
> method needs to do its job.  While this is not always practical (somebody
> has to access module-level variables), it makes the method easier to
> maintain and easier to reuse.  Using variables that are not passed in
> thru the method signature creates what is called "coupling" and is
> generally bad.
>
> R
>
> > I was wondering, I'm looking at this guys code and he has a global 
> public variant declared in a module.  Then in one of his forms he has an 
> event that passes that same global variable to a function.  I'm guessing 
> the variable wasn't always global and that for whatever reason the guy 
> moved it without modifying all his functions.  But for future reference, 
> I was wondering if you gain anything by passing the variable instead of 
> relying on the global scope.  This piece of information could come in 
> handy if I'm ever stuck with a program that heavily relies on global 
> variables.
>
> Thanks,
>
> Jerry


  Return to Index