March 28, 2007

My Cheat Code to Create Properties in VB6

Did you ever feel lazy to type the same code with a small modification but it is every where. And doing the same work repeatedly for let say 34 times? I will feel like my hand got the RSI (Repetitive Strain Injury). The most common task that I encounter this situation is when creating a set of properties for any classes. As the time goes I discover a cheap cheat code to reduce my coding work and let some script do my repeated work for me...

Here is my code to create a set of 17 properties in a class...

Public Sub MakePropCode()
Dim vsProp
Dim vlProp
Dim vbProp
Dim i As Long
Dim sOutPut As String

' array of string properties
vsProp = Array("ISNFolder", "ISDFolder", "CSVFolder", _
"DBHost", "DBUser", "DBPassword", _
"RDBHost", "RDBUser", "RDBPassword", _
"LogPath", "DRSMHost", "DRSMID", "DRSMPassword")

' array of long properties
vlProp = Array("SeqInterval", "SeqType")

' array of boolean properties
vbProp = Array("WriteLog")

sOutPut = "Option Explicit" & vbCrLf & vbCrLf

' private declaration for string property
For i = 0 To UBound(vsProp)
sOutPut = sOutPut & "Private m_s" & vsProp(i) & _
" As String" & vbCrLf
Next

' private declaration for long property
For i = 0 To UBound(vlProp)
sOutPut = sOutPut & "Private m_l" & vlProp(i) & _
" As Long" & vbCrLf
Next

' private declaration for boolean property
For i = 0 To UBound(vbProp)
sOutPut = sOutPut & "Private m_b" & vbProp(i) & _
" As Boolean" & vbCrLf
Next

sOutPut = sOutPut & vbCrLf & vbCrLf

' public get & let declaration for string prop

For i = 0 To UBound(vsProp)
sOutPut = sOutPut & "Public Property Get " & vsProp(i) & _
"() As String" & vbCrLf
sOutPut = sOutPut & " " & vsProp(i) & " = m_s" & _
vsProp(i) & vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
sOutPut = sOutPut & "Public Property Let " & vsProp(i) & _
"(ByVal sNew As String)" & vbCrLf
sOutPut = sOutPut & " m_s" & vsProp(i) & " = sNew" & _
vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
Next

' public get & let declaration for long prop

For i = 0 To UBound(vlProp)
sOutPut = sOutPut & "Public Property Get " & vlProp(i) & _
"() As Long" & vbCrLf
sOutPut = sOutPut & " " & vlProp(i) & " = m_l" & _
vlProp(i) & vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
sOutPut = sOutPut & "Public Property Let " & vlProp(i) & _
"(ByVal lNew As Long)" & vbCrLf
sOutPut = sOutPut & " m_l" & vlProp(i) & " = lNew" & _
vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
Next

' public get & let declaration for boolean prop

For i = 0 To UBound(vbProp)
sOutPut = sOutPut & "Public Property Get " & vbProp(i) & _
"() As Boolean" & vbCrLf
sOutPut = sOutPut & " " & vbProp(i) & " = m_b" & _
vbProp(i) & vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
sOutPut = sOutPut & "Public Property Let " & vlProp(i) & _
"(ByVal bNew As Boolean)" & vbCrLf
sOutPut = sOutPut & " m_b" & vbProp(i) & " = bNew" & _
vbCrLf
sOutPut = sOutPut & "End Property" & vbCrLf & vbCrLf
Next

' write it to a text file in the same directory of the project
Open App.Path & "\prop.txt" For Output As #1
Print #1, sOutPut
Close #1

Debug.Print "Finished!"

End Sub

How to use it? The way that I'm using it is by creating one extra module in my project which I can delete later. Paste the code and modify the Property Names with their type array. then type the function name on the Immediate Window and press enter. Tadaaa!! My properties set of property get and property let is created in a text file called prop.txt

This is the text file output of this function:

Option Explicit

Private m_sISNFolder As String
Private
m_sISDFolder As String
Private
m_sCSVFolder As String
Private
m_sDBHost As String
Private
m_sDBUser As String
Private
m_sDBPassword As String
Private
m_sRDBHost As String
Private
m_sRDBUser As String
Private
m_sRDBPassword As String
Private
m_sLogPath As String
Private
m_sDRSMHost As String
Private
m_sDRSMID As String
Private
m_sDRSMPassword As String
Private
m_lSeqInterval As Long
Private
m_lSeqType As Long
Private
m_bWriteLog As Boolean


Public Property Get ISNFolder() As String
ISNFolder = m_sISNFolder
End Property

Public Property Let ISNFolder(ByVal sNew As String)
m_sISNFolder = sNew
End Property

Public Property Get ISDFolder() As String
ISDFolder = m_sISDFolder
End Property

Public Property Let ISDFolder(ByVal sNew As String)
m_sISDFolder = sNew
End Property

Public Property Get CSVFolder() As String
CSVFolder = m_sCSVFolder
End Property

Public Property Let CSVFolder(ByVal sNew As String)
m_sCSVFolder = sNew
End Property

Public Property Get DBHost() As String
DBHost = m_sDBHost
End Property

Public Property Let DBHost(ByVal sNew As String)
m_sDBHost = sNew
End Property

Public Property Get DBUser() As String
DBUser = m_sDBUser
End Property

Public Property Let DBUser(ByVal sNew As String)
m_sDBUser = sNew
End Property

Public Property Get DBPassword() As String
DBPassword = m_sDBPassword
End Property

Public Property Let DBPassword(ByVal sNew As String)
m_sDBPassword = sNew
End Property

Public Property Get RDBHost() As String
RDBHost = m_sRDBHost
End Property

Public Property Let RDBHost(ByVal sNew As String)
m_sRDBHost = sNew
End Property

Public Property Get RDBUser() As String
RDBUser = m_sRDBUser
End Property

Public Property Let RDBUser(ByVal sNew As String)
m_sRDBUser = sNew
End Property

Public Property Get RDBPassword() As String
RDBPassword = m_sRDBPassword
End Property

Public Property Let RDBPassword(ByVal sNew As String)
m_sRDBPassword = sNew
End Property

Public Property Get LogPath() As String
LogPath = m_sLogPath
End Property

Public Property Let LogPath(ByVal sNew As String)
m_sLogPath = sNew
End Property

Public Property Get DRSMHost() As String
DRSMHost = m_sDRSMHost
End Property

Public Property Let DRSMHost(ByVal sNew As String)
m_sDRSMHost = sNew
End Property

Public Property Get DRSMID() As String
DRSMID = m_sDRSMID
End Property

Public Property Let DRSMID(ByVal sNew As String)
m_sDRSMID = sNew
End Property

Public Property Get DRSMPassword() As String
DRSMPassword = m_sDRSMPassword
End Property

Public Property Let DRSMPassword(ByVal sNew As String)
m_sDRSMPassword = sNew
End Property

Public Property Get SeqInterval() As Long
SeqInterval = m_lSeqInterval
End Property

Public Property Let SeqInterval(ByVal lNew As Long)
m_lSeqInterval = lNew
End Property

Public Property Get SeqType() As Long
SeqType = m_lSeqType
End Property

Public Property Let SeqType(ByVal lNew As Long)
m_lSeqType = lNew
End Property

Public Property Get WriteLog() As Boolean
WriteLog = m_bWriteLog
End Property

Public Property Let SeqInterval(ByVal bNew As Boolean)
m_bWriteLog = bNew
End Property

Since I have this code, I don't have to code like this again and again. Simply copy and paste, then edit the appropriate line. Nasty cheat code right? You can freely use and distribute this code but please credit my name if you post it somewhere on the net and please link to this page too. Have Fun!

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

1 comments:

Interpreter of the Obvious said...

Thanks for the tip. I have started writing something similar that also takes an RDBMS table name and creates SQL statements for inclusion into a VB6 class. Like you, I am weary of manually recreating (via the clipboard, ha ha) databased classes.