Consolidate Global Data

We all know that using global variables in our projects is not considered good programming practice. Nevertheless, everyone does it. Sometimes you need to introduce public variables for storing initialization (INI) file names, Registry keys, shared constant values, and other common needs. Well, you can consolidate all your public variables through one public class, which encapsulates your variables. Store global data inside this class as module-level variables and constants or public Enums:

 Option ExplicitPrivate m_sINIFile As StringPrivate Const QUERY_TIMEOUT As Long = 120Private Sub Class_Initialize()	m_sINIFile = App.Path & "" & App.Title & _		".ini"End SubPublic Property Get INIFile() As String	INIFile = m_sINIFileEnd PropertyPublic Property Get REGKey() As String	REGKey = "SOFTWAREMyKey"End PropertyPublic Property Get QueryTimeOut() As Long	QueryTimeOut = QUERY_TIMEOUTEnd Property

Set the class’s Instancing property to Private, so it’s not visible to outside projects. After you expose the variables as properties or enumerations of a class, you can create this class on a module level or on a project level and use the data stored in this class:

 Dim objData As CProjectDataSet objData = New CProjectDataIf Dir$(objData.INIFile, vbNormal) = "" Then	MsgBox objData.INIFile _		& " - INI file not found!"End If

By doing this, you don’t have to memorize the names of those public variables. As you type the name of your data class, VB’s IntelliSense helps you pick the class properties you need.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: