Macros in VS.NET
I have to admit I
haven't taken advantage of enough of VS.NET's capabilities... the ability to
write macros alone should have resulted in a ton of useful little routines, but
I have only written a few. One of the ones I use most often converts between a
list of my internal member variables in a class...
Dim m_fred As String
Dim m_counter As Integer
to the bare-bones properties
Public Property fred() As String
Get
Return m_fred
End Get
Set(ByVal Value As String)
m_fred = Value
End Set
End Property
Public Property counter() As Integer
Get
Return m_counter
End Get
Set(ByVal Value As Integer)
m_counter = Value
End Set
End Property
It is dependent on my particular hungarian-ish (m_) naming
style for internal variables and it doesn't deal well with arrays or
variables that get instantiated in their declarations... but I find
it a real timesaver to spit out that initial pass at the
properties before I go in and add any validation or whatever else
I was going to do... On the off chance that you might find it useful as
well, or that you want to "finish it up", here is the source of the
macro:
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 As String
Dim publicName As String
Dim dataType As String
Dim propertyProcedure As String
Dim r As Regex
r = New Regex( _
"(Dim|Private)\s*(?<varname>\S*)\s*As\s*(?<typename>\S*)", _
RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
For Each line In lines
line = line.Trim
If Not line = "" Then
Dim mtch As 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
'don't do anything
'but I don't want to see an error!
End Try
DTE.UndoContext.Close()
End Sub
Anyone have some real cool/useful VS.NET macros?