All pastes #121989 Raw Edit

Someone

public text v1 · immutable
·published 2006-08-08 10:12 UTC
rendered paste body
Imports EnvDTE
Imports System.Diagnostics

Public Module ClassMacros
    Sub ConvertProperties()
        DTE.UndoContext.Open("ConvertProperties")
        Try
            Dim txt As TextSelection
            txt = DTE.ActiveDocument.Selection

            Dim line, originalCode As String
            originalCode = txt.Text

            Dim lines() As String
            lines = Split(originalCode, vbLf)

            Dim variableName, publicName, dataType, propertyProcedure As String

            Dim r As System.Text.RegularExpressions.Regex
            r = New System.Text.RegularExpressions.Regex( _
                    "(Dim|Private)\s*(?<varname>\S*)\s*As\s*(?<typename>\S*)", _
                    System.Text.RegularExpressions.RegexOptions.IgnoreCase _
                    Or System.Text.RegularExpressions.RegexOptions.IgnoreCase.ExplicitCapture)

            For Each line In lines
                line = line.Trim
                If Not line = "" Then
                    Dim mtch As System.Text.RegularExpressions.Match
                    mtch = r.Match(line)

                    If mtch.Success Then
                        variableName = mtch.Groups("varname").Value.Trim
                        dataType = mtch.Groups("typename").Value.Trim
                        publicName = variableName.Substring(2)

                        propertyProcedure = _
                            String.Format( _
                                 "{0}Public Property {1} As {2}{0}" _
                                & "    Get{0}" _
                                & "        Return {3}{0}" _
                                & "    End Get{0}" _
                                & "    Set(ByVal Value As {2}){0}" _
                                & "        {3} = Value{0}" _
                                & "    End Set{0}" _
                                & "End Property", vbCrLf, publicName, _
                                dataType, variableName)

                        txt.Insert(vbCrLf & propertyProcedure, _
                            vsInsertFlags.vsInsertFlagsInsertAtEnd)
                    End If

                End If
            Next
            txt.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

End Module