This marco will allow you to create property wrapers for your varibles.
Video
Instructions
- In VS.NET start the macros ide
- Next create a new code file by clicking "Project -> Add new item"
- Then copy and paste the code below in the code file you just created
Private Sub CreateProp(Optional ByVal DoGet As Boolean = True, _
Optional ByVal DoSet As Boolean = True)
Dim TD As TextDocument
Dim strVarible, strCode, strType, strName As String
Dim lngOrigLine As Integer
TD = CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)
lngOrigLine = TD.Selection.TopPoint.Line
strVarible = TD.Selection.Text.Trim
If strVarible.Length = 0 Then
Dim strVName As String
strVName = InputBox("Specify the name of the varible to wrap ...").Trim
If strVName.Length = 0 Then Exit Sub
Dim strVType As String
strVType = InputBox("Specify the name of the data type for the property ...")
If strVType.Length = 0 Then Exit Sub
strVarible = strVName & " As " & strVType
End If
If strVarible.ToUpper.IndexOf(" AS ") > 0 Then
strType = strVarible.Substring(strVarible.ToUpper.IndexOf(" AS ") 4).Trim
If strType.Trim.ToLower.StartsWith("new ") Then
strType = strType.Substring(strType.Trim.ToLower.IndexOf("new ") 4)
End If
If strType.Trim.ToLower.IndexOf("(") <> -1 Then
strType = strType.Substring(0, strType.Trim.ToLower.IndexOf("("))
End If
strVarible = strVarible.Substring(0, strVarible.ToUpper.IndexOf(" AS ")).Trim
Else
strType = InputBox("Selection does not encompas '... As >Type<'")
If strType.Length = 0 Then Exit Sub
End If
strName = InputBox("Specify the name of the property genereate ...", , strVarible).Trim
If strName.Length = 0 Then Exit Sub
strCode = vbCrLf
If DoGet And DoSet Then
strCode &= " Public Property " & strName & "() As " & strType & vbCrLf
ElseIf DoGet And DoSet = False Then
strCode &= " Public ReadOnly Property " & strName & "() As " & strType & vbCrLf
ElseIf DoGet = False And DoSet Then
strCode &= " Public WriteOnly Property " & strName & "() As " & strType & vbCrLf
Else
strCode &= " Public Property " & strName & "() As " & strType & vbCrLf
End If
If DoGet Then
strCode &= " Get" & vbCrLf
strCode &= " Return " & strVarible & vbCrLf
strCode &= " End Get" & vbCrLf
End If
If DoSet Then
strCode &= " Set(Byval Value As " & strType & ")" & vbCrLf
strCode &= " " & strVarible & " = Value" & vbCrLf
strCode &= " End Set" & vbCrLf
End If
strCode &= " End Property" & vbCrLf
'
'Copy property
'
Dim ts As TextSelection = TD.Selection
Dim ce As CodeElement
Dim te As EnvDTE.TextPoint
While ce Is Nothing
ce = ts.ActivePoint.CodeElement(vsCMElement.vsCMElementClass)
If ce Is Nothing Then
ce = ts.ActivePoint.CodeElement(vsCMElement.vsCMElementStruct)
If ce Is Nothing Then
ts.StartOfLine()
ts.LineDown()
End If
End If
End While
te = ce.GetEndPoint(vsCMPart.vsCMPartBody)
TD.Selection.MoveToLineAndOffset(te.Line, 0, False)
TD.Selection.Insert(strCode)
TD.Selection.GotoLine(lngOrigLine)
TD.Selection.StartOfLine()
DoFormatDoc()
End Sub
Sub CreateGetSetProperty()
CreateProp(True, True)
End Sub
Sub CreateGetProperty()
CreateProp(True, False)
End Sub
Sub CreateSetProperty()
CreateProp(False, True)
End Sub
|
Now you can use the "CreateGetSetProperty", "CreateGetProperty", and "CreateSetProperty"
methods to add wrapper properties for any varible.
|