Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Property Procedures in BAS modules

Total Hit ( 3499)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Visual Basic supports Property procedures to implement properties in your own CLS and FRM modules. However, it turns out that you can use Property procedures even within BAS code modules.

How can this improve your programming skills? In many a way, as I will demonstrate. Say your program includes a global Integer variable named ThisMonth, which is supposed to contain a value in the range 1 to 12:

Click here to copy the following block
Public ThisMonth As Integer

How can you be sure that this variable is never assigned a value outside that range? The answer is to turn the actual variable into a Private member of the module, and use a pair of Property procedures:

Click here to copy the following block
' within a BAS module
Private m_ThisMonth As Integer

Public Property Get ThisMonth() As Integer
  ThisMonth = m_ThisMonth
End Property
Public Property Let ThisMonth(newValue As Integer)
  If newValue < 1 Or newValue > 12 Then
    Err.Raise 999, "BAS module", "Invalid value for ThisMonth"
  End If
  m_ThisMonth = newValue
End Property

A benefit of this approach is that the new Property ThisName has the same name as the original global variable, therefore you don't need to modify one single line elsewhere in the source code. Once you complete the test of the application, you can delete the two Property procedures and restore the old global variable, again with no change to the rest of the program.
Another advantage of Property procedures within BAS modules is that you can expose the same value under different forms. For instance you could add this new read-only property, that exposes ThisMonth in the form of a string:

Click here to copy the following block
Public Property ThisMonthName() As String
  ThisMonthName = Choose(m_ThisMonth, "Jan", "Feb", "Mar", "Apr", "May", _
    "Jun", "Jul", "Agu", "Sep", "Oct", "Nov", "Dec")
End Property

You can use Property procedures in BAS modules also to implement more flexible symbolic constants. Since you cannot using neither string concatenation nor Chr$() functions within Const statements, you are prevented from declaring string constants that embed control (unprintable) character. For instance, the following line raises a compile-time error:

Click here to copy the following block
' this statement is invalid !
Const EscapeChar = Chr$(27)

However, you can simulate such a string constant by using a Property Get procedure within a BAS module, as follows:

Click here to copy the following block
Public Property Get EscapeChar() As String
  EscapeChar = Chr$(27)
End Property

Please note that this is a bit less efficient than a "true" symbolic constant, since the value is evaluated each time the "constant" is used from elsewhere in the program. On the other hand, this approach is safer than using a Public variable initialized in your Main procedure, because the value returned by the DoubleCRLF property cannot by accidentally modified elsewhere in the program.
Of course, The same effect can be reached using a Function instead of a Property Get procedure, but Property procedures are in general more flexible. For instance, you can easily implement "constants" whose value is set at runtime, as in:

Click here to copy the following block
Private m_Password As String
Public Property Get Password() As String
  Password = m_Password
End Property
Public Property Let Password(newValue As String)
  If m_Password = "" Then
    m_Password = newValue
  Else
    Err.Raise 1001, "BAS module", "Password already assigned"
  End If
End Property

UPDATE: Davod Parvin correctly suggests that you don't even need to manually change the implementation of the property when switching from the debug phase to the final compiled program, because you can use compiler directives, as in:

Click here to copy the following block
#Const Debug = -1

#If Debug = 0 Then

Public Password As String

#Else

Private m_Password As String
Public Property Get Password() As String
  Password = m_Password
End Property
Public Property Let Password(newValue As String)
  If m_Password = "" Then
    m_Password = newValue
  Else
    Err.Raise 1001, "BAS module", "Password already assigned"
  End If
End Property

#End If

Update: The original tip uncorrectly stated that the "&" operator in a Const statement was illegal. It was in some previous version of VB, but at least in VB6 it works perfectly. Thanks to Rich M. who spotted the error and let us correct it.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.