John W. Colby
jcolby at colbyconsulting.com
Sat Mar 15 11:04:00 CST 2003
Mark, I also use globals for the db connection. This is a case where the connection is specific, a penalty is paid for creating / deleting it, and it is needed all over the app. The connection is not something that is going to be updated (written to) by pieces of the application, it is going to be created once and then used all over. That is a good candidate for a global. Globals tend to cause problems when they are *updated* from all over the place, and the reason is that is quite difficult to track down who is doing the updating when it is incorrectly updated. If for example a serial port is opened and closed by 10 different functions, one function tries to use the serial port and it is closed. Who closed it? It's supposed to be opened, but some process closed it unexpectedly. These kinds of problems can be a real bear to track down and fix. It becomes a bigger problem if the system is written and maintained by a team of programmers where nobody knows the whole story. Globals by definition are available to anyone at any time. Scope exists for a reason. If a variable is only used by a specific function, then it's variables have no business being global to the program (or even the module). If a variable is only used by code inside an instance of a class but needs to be accessed by several functions inside the class, then it should be defined inside the class (in the header) as a PRIVATE variable. Thus only code inside the class can modify the variable. If a variable needs to be shared by many functions inside of a module, but only these functions modify the variable, then the variable should be PRIVATE to that module. By making any of these variables truly Global to the project, you end up with things being (able to be) set by code that has no business setting the variable. On the other hand, if as in your example, a database connection is needed, and a function calls a function which calls a function, which needs that connection, why pass the connection through 3 functions that don't even need the connection simply so that the fourth function that does need it can have access to the connection? In a case like this, I tend to make the connection a private variable inside a module, with a function that initializes the variable when the db opens, and then define a function that READS the connection variable and returns the value. This in effect makes it read only. Anyone can call the function that gets the value, but since the variable is PRIVATE to the module, no one outside the module can write to it. John W. Colby Colby Consulting www.ColbyConsulting.com -----Original Message----- From: accessd-admin at databaseadvisors.com [mailto:accessd-admin at databaseadvisors.com]On Behalf Of Mark L. Breen Sent: Friday, March 14, 2003 12:37 PM To: accessd at databaseadvisors.com Subject: Re: [AccessD] Using Global Connctions Hello All, I was not aware that a previous debate (sounds a little heated) had been already carried out. Sorry for duplicating the discussion. In the last few emails, Marcus was the only one that explicitly stated that he likes the idea of using Globals for a db connection. So leaving aside passing around an order nunber or a customer id as a global (which I would never even consider), what do you guys do to hand around a database connection? Do you use Globals or do you make the connction each time or do you use some kind of class. Any sample code would be interesting to read. What I do is have the following code in a module ' This will be the global connection that we will share, we open it once only and share it throughout the applications life Global gccnn As New ADODB.Connection Global Const gcstrConStr As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=spps40;Data Source=PC1" and then in the first form_load I have ' Give it a connect string gccnn.ConnectionString = gcstrConStr ' And make the connection gccnn.Open What do you guys think of this strategy, should I use a class to pass it around. And if I am not using a class, do you think that it is a little bit untidy or is downright bad practice. Again, I am just curious what the rest of the world is doing in their apps. Thanks Mark _______________________________________________ AccessD mailing list AccessD at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/accessd Website: http://www.databaseadvisors.com ---------------------------------------------------- Is email taking over your day? Manage your time with eMailBoss. Try it free! http://www.eMailBoss.com -------------- next part -------------- A non-text attachment was scrubbed... Name: winmail.dat Type: application/ms-tnef Size: 3908 bytes Desc: not available URL: <http://databaseadvisors.com/pipermail/accessd/attachments/20030315/3d5a0c3d/attachment-0001.bin>