A Portion of Buff

Everybody else had one, so...

Macro-economics

After seeing this macro for VB.Net, I thought I'd be clever and adapt it for C#. Then I saw this, so I needn't have bothered, but my version handles a slightly different format, so if you write your private fields like so:

SomeType foo; //_foo is OK as well - it will strip the leading underscore for the public name

it will create properties of the form:

public TypeName Foo 
{
get { return foo; }
set { foo = value; }
}
It's a bit buggy (it will try to create properties from public fields and output invalid code, and doesn't match the indentation of the private fields), but someone might find it useful:

Imports EnvDTE
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module ConvertToProperties
    Sub Run()

        DTE.UndoContext.Open("ConvertToProperties")

        Try
            Dim text As TextSelection = DTE.ActiveDocument.Selection
            Dim line As String
            Dim lines() As String = Split(text.Text, vbLf)
            Dim match As Match

            Dim r As Regex = New Regex("\s*(?(private|protected)*)\s*(?\S*)\s*(?\S*)", RegexOptions.IgnoreCase Or RegexOptions.IgnoreCase.ExplicitCapture)

            text.Insert(vbCrLf, vsInsertFlags.vsInsertFlagsInsertAtEnd)

            For Each line In lines
                line = line.Trim
                If Not line = "" Then
                    match = r.Match(line)
                    If match.Success Then
                        CreateProperty(text, line, match)
                    End If
                End If
            Next

            text.SmartFormat()

        Catch ex As System.Exception

            MsgBox("There was an error while constructing Property code: " & ex.ToString & ".", MsgBoxStyle.Critical, "ConvertProperties")

        End Try

        DTE.UndoContext.Close()

    End Sub
    Sub CreateProperty(ByVal text As TextSelection, ByVal line As String, ByVal match As Match)

        Dim fieldName As String
        Dim strippedVariableName As String
        Dim publicName As String
        Dim typeName As String
        Dim startChars As Char = ("_")
        Dim endChars As Char = (";")

        fieldName = match.Groups("fieldName").Value.Trim
        fieldName = fieldName.TrimEnd(endChars)

        strippedVariableName = fieldName.TrimStart(startChars)

        typeName = match.Groups("typeName").Value.Trim
        publicName = strippedVariableName.Substring(0, 1).ToUpper & strippedVariableName.Substring(1)

        WriteProperty(text, fieldName, typeName, publicName)

    End Sub
    Sub WriteProperty(ByVal text As TextSelection, ByVal fieldName As String, ByVal typeName As String, ByVal publicName As String)

        Dim propertyText As String = String.Format("public {0} {1}" _
                                                        & "{3}{{{3}" _
                                                        & "    get {{ return {2}; }}{3}" _
                                                        & "    set {{ {2} = value; }}{3}" _
                                                        & "}}{3}", _
                                                        typeName, _
                                                        publicName, _
                                                        fieldName, _
                                                        vbCrLf)

        text.Insert(vbCrLf & propertyText, vsInsertFlags.vsInsertFlagsInsertAtEnd)

    End Sub
End Module

Comments

Oddur Magnusson said:

Check out QuickCode, it does the same, but also a lot more. And is a while lot smoother.

typing in:
propv string name

and pressing alt-q is all there is to it !

http://quickcode.dvxp.com/
# April 29, 2003 2:41 AM

Jim Arnold said:

That's as maybe, but it's <grr>bother-ware</grr>.

Jim
# April 29, 2003 4:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)