Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGenerate code for standard property procedures in VB .NET
DescriptionThis example shows how to generate code for standard property procedures in VB .NET.
Keywordsproperty procedure, get, set, replace, token
CategoriesTips and Tricks, Utilities, Software Engineering
 
This example builds a string representing a standard property procedure. 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 btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Const TEMPLATE As String = _
        "Private m_@name@ As @type@ = @default@" & vbCrLf & _
            _
        "Public Property @name@() As @type@" & vbCrLf & _
        "    Get" & vbCrLf & _
        "        Return m_@name@" & vbCrLf & _
        "    End Get" & vbCrLf & _
        "    Set(ByVal new_value AS @type@)" & vbCrLf & _
        "        m_@name@ = new_value" & vbCrLf & _
        "    End Set" & vbCrLf & _
        "End Property" & vbCrLf
    Dim result As String = TEMPLATE

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

    ' Compose the comments.
    Dim lines() As String = Split$(txtComments.Text, vbCrLf)
    Dim comments As String = ""
    For i As Integer = 0 To lines.GetUpperBound(0)
        comments &= "' " & lines(i) & vbCrLf
    Next i

    txtResult.Text = comments & result
    txtResult.Select()
    'txtResult.Focus()
End Sub
 
This program doesn't handle every possible property procedure. For example, it's not designed to build parameterized property 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.
  Updated