Tuesday, June 8, 2010

A better Property Macro Snippet

Private Function MakePropertyGet(ByVal line As String) As String
'Make sure we are dealing with selected text in the form of:
'Variable_Name Type Default_Value
Dim Words() As String = line.Trim().Split()

If Words.Length < 2 Then
Return "Input in invalid format! Use PropName Type Default_Value"
Else


Return "public " & Words(0) & " " & Words(1) & Words(0) & vbNewLine & _
vbTab & vbTab & "{" & vbNewLine & _
vbTab & vbTab & vbTab & "get { return " & Words(1) & "; }" & vbNewLine & _
vbTab & vbTab & "}"

End If

End Function

Public Sub CreateSimplePropertyVB()
Dim TS As TextSelection = DTE.ActiveDocument.Selection
Dim Insertion As String, Line As String
Dim Lines() As String = TS.Text.Split(vbNewLine)

For Each Line In Lines
Insertion &= MakePropertyGet(Line)
Next

TS.Delete()
TS.Insert(Insertion)
End Sub

No comments:

Post a Comment