Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Code Repository
   

Macro - Auto-Generate Public Properties



 
Return

Macro - Auto-Generate Public Properties

Would you like to be able to highlight a list of private variables and auto-generate a list of public properties?

Here you go:

To use the macro below copy it into your macro file. Then highlight one or more private variables and double click the "CreatePublicProperties" macro in your macro list.

The naming convention used for your private variables in your code that you then apply this macro to should use a leading underscore.
  1. Private _MyString As String
  2. Private _MyInt As Int32

  1. Public Sub CreatePublicProperties()
  2. Dim TS As TextSelection = GetTextSelection()
  3. Dim Insertion As New System.Text.StringBuilder
  4. Dim Line As String
  5. Dim Lines() As String = TS.Text.Split(vbNewLine)
  6. Dim ReturnedText As String
  7. Dim LineCount, LineLoop As System.Int32
  8. LineCount = Lines.GetUpperBound(0)
  9. For LineLoop = 0 To LineCount
  10. ReturnedText = GetPropertyInsertion(Lines.GetValue(LineLoop))
  11. Insertion.Append(ReturnedText)
  12. Insertion.Append(InsertLine(LineLoop, LineCount, ReturnedText))
  13. Next
  14. TS.EndOfLine()
  15. TS.LineDown(False, 1)
  16. TS.EndOfLine()
  17. TS.NewLine(2)
  18. TS.StartOfLine()
  19. TS.Text = "#Region ""Public Properties"""
  20. TS.NewLine()
  21. TS.Insert(Insertion.ToString)
  22. TS.LineDown(False, 2)
  23. End Sub
  24. Private Function GetPropertyInsertion(ByVal text As String) As String
  25. Dim Words() As String = text.Trim.Split()
  26. ' Check if the line is a comment line.
  27. If Words(0).IndexOf("'") = 0 Then
  28. '---This is a comment line include it.
  29. Return text
  30. Else
  31. If Words.Length > 3 Then
  32. '---This is a variable declaration
  33. Dim Insertion As New System.Text.StringBuilder
  34. Insertion.Append(vbTab & "Public Property " & Words(1).Trim("_"))
  35. Insertion.Append(" As " & Words(3))
  36. Insertion.Append(vbNewLine)
  37. Insertion.Append(vbTab & vbTab & "Get")
  38. Insertion.Append(vbNewLine)
  39. Insertion.Append(vbTab & vbTab & vbTab & "Return " & Words(1))
  40. Insertion.Append(vbNewLine)
  41. Insertion.Append(vbTab & vbTab & "End Get")
  42. Insertion.Append(vbNewLine)
  43. Insertion.Append(vbTab & vbTab & "Set(ByVal Value As " & Words(3) & ")")
  44. Insertion.Append(vbNewLine)
  45. Insertion.Append(vbTab & vbTab & vbTab & Words(1) & " = Value")
  46. Insertion.Append(vbNewLine)
  47. Insertion.Append(vbTab & vbTab & "End Set")
  48. Insertion.Append(vbNewLine)
  49. Insertion.Append(vbTab & "End Property")
  50. Return Insertion.ToString
  51. End If
  52. End If
  53. Return ""
  54. End Function
  55. Private Function InsertLine(ByVal lineLoop As Integer, ByVal lineCount As Integer, ByVal returnedText As String) As String
  56. If (lineLoop < lineCount) And (returnedText > "") Then
  57. Return vbNewLine
  58. End If
  59. Return ""
  60. End Function
  61. Private Function GetTextSelection() As EnvDTE.TextSelection
  62. Return DTE.ActiveDocument.Selection
  63. End Function


Return
Microsoft Certified Professional   © 2017 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com