Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 13, 1999

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 Explicit

Private m_sINIFile As String
Private Const QUERY_TIMEOUT As Long = 120

Private Sub Class_Initialize()
	m_sINIFile = App.Path & "\" & App.Title & _
		".ini"
End Sub

Public Property Get INIFile() As String
	INIFile = m_sINIFile
End Property

Public Property Get REGKey() As String
	REGKey = "\SOFTWARE\MyKey"
End Property

Public Property Get QueryTimeOut() As Long
	QueryTimeOut = QUERY_TIMEOUT
End 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 CProjectData
Set objData = New CProjectData

If 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.

Oleg Melnikov
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date