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?
Comments
- Anonymous
March 21, 2003
VB.NET Property code generation macro in VS.NET. : Jan Tielens' Bloggings - Anonymous
March 22, 2003
Duncan, have you looked at QuickCode .NET? I use this instead of VS.NET macros. There are several "expansions" defined, including one for properties as you describe above. - Anonymous
March 22, 2003
ooh, QuickCode.NET looks pretty neat. I'll have to try it! - Anonymous
May 05, 2003
Chad, wow that is cool! Thanks! - Anonymous
April 13, 2004
The 2 below let you quickly comment out and uncomment (VB.NET) all of the Response.Writes in a file. Great for debugging ASP.NET pages.
Sub CommentResponseDotWrites()
Dim selection As TextSelection = DTE.ActiveDocument.Selection()
Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
Dim endpt As TextPoint = selection.BottomPoint
Dim commentStart As String = "' "
Dim txtSel As TextSelection
DTE.UndoContext.Open("Comment Region")
Try
Do While (start.LessThan(endpt))
If InStr(start.GetText(start.LineLength).ToLower, "response.write") Then
start.Unindent()
start.Insert(commentStart)
End If
start.LineDown()
start.StartOfLine()
Loop
Finally
'If an error occured, then need to make sure that the undo context is cleaned up.
'Otherwise, the editor can be left in a perpetual undo context
DTE.UndoContext.Close()
End Try
End Sub
Sub UnCommentResponseDotWrites()
Dim selection As TextSelection = DTE.ActiveDocument.Selection()
Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
Dim endpt As TextPoint = selection.BottomPoint
Dim commentStart As String = "' "
DTE.UndoContext.Open("Comment Region")
Try
Do While (start.LessThan(endpt))
Dim intComment As Integer
intComment = InStr(start.GetText(start.LineLength).ToLower, "' response.write")
If intComment <> 0 Then
'start.ReplaceText(intComment, "", 0)
start.Delete(intComment)
End If
start.LineDown()
start.StartOfLine()
Loop
Finally
'If an error occured, then need to make sure that the undo context is cleaned up.
'Otherwise, the editor can be left in a perpetual undo context
DTE.UndoContext.Close()
End Try
End Sub