What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGenerate code for standard property procedures
DescriptionThis example shows how to generate code for standard property procedures in Visual Basic 6.
Keywordsproperty procedure, get, set, replace, token
CategoriesTips and Tricks, Utilities, Software Engineering
This example builds a string representing a standard pair of property procedures. When you click Go, the program replaces tokens in a template string to create a private variable, and property Let and Get procedures. It displays the results and selects them so it's easy to copy and paste them into a program.
Private Sub cmdGo_Click()
Const TEMPLATE As String = _
    "' Declare storage for the @name@ property." & vbCrLf & _
    "Private m_@name@ As @type@" & vbCrLf & _
    "' Set the @name@ property." & vbCrLf & _
    "Public Property Let @name@(ByVal new_value As @type@)" _
        & vbCrLf & _
    "    m_@name@ = new_value" & vbCrLf & _
    "End Property" & vbCrLf & _
    "' Get the @name@ property." & vbCrLf & _
    "Public Property Get @name@() As @type@" & vbCrLf & _
    "    @name@ = m_@name@" & vbCrLf & _
    "End Property"
Dim result As String
Dim lines() As String
Dim comments As String
Dim i As Integer

    ' Fill in the main fields.
    result = TEMPLATE
    result = Replace(result, "@name@", txtName.Text)
    result = Replace(result, "@type@", cboDataType.Text)

    ' Compose the comments.
    lines = Split(txtComments.Text, vbCrLf)
    comments = ""
    For i = LBound(lines) To UBound(lines)
        comments = comments & "' " & lines(i) & vbCrLf
    Next i

    txtResult.Text = comments & result
    txtResult.SelStart = 0
    txtResult.SelLength = Len(txtResult.Text)
End Sub
Here's some example output.
' The Employee's first name.
' Declare storage for the FirstName property.
Private m_FirstName As String

' Set the FirstName property.
Public Property Let FirstName(ByVal new_value As String)
    m_FirstName = new_value
End Property

' Get the FirstName property.
Public Property Get FirstName() As String
    FirstName = m_FirstName
End Property
This program doesn't handle every possible property procedure. It's not designed to build parameterized property procedures, property Set procedures, or procedures with some data types, but it does give you a head start in those cases.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.