Using a Macro to Create Properties for Member Variables

Provided by the FMS Development Team

While designing classes, one of the most boring tasks is to create properties for member variables. Here is a macro (more about macros here) that helps with that.

Code

Here is the code that creates the property:

Public Sub FMS_MakeProperty()

    Dim edtrWin As Window = Nothing
    Dim prjItm As ProjectItem = Nothing
    Dim txtDoc As TextDocument = Nothing
    Dim cc As CodeElement = Nothing
    Dim cv As CodeVariable = Nothing
    Dim varName As String = String.Empty
    Dim varType As String = String.Empty
    Dim linProp As String = String.Empty

    Try
      ' get the active editor

 
     edtrWin = DTE.ActiveDocument.ActiveWindow

      If edtrWin Is Nothing Then
        DTE.StatusBar.Text = "No text editor open."
        Return
      End If

      prjItm = edtrWin.ProjectItem

      ' this may happen if the document is
      ' not part of the project
      If prjItm Is Nothing Then
        DTE.StatusBar.Text = "Projectitem is null !!"
        Return
      End If

      ' get the textdocument associated with this
 
    
txtDoc = prjItm.Document.Object("TextDocument")

      ' get the code element from the user location
      cc = prjItm.FileCodeModel.CodeElementFromPoint
         (txtDoc.Selection.AnchorPoint, _ vsCMElement.vsCMElementVariable)

      If cc Is Nothing Then
        DTE.StatusBar.Text = "Please position cursor on a member variable.."
        Return
      End If
 

      cv = CType(cc, CodeVariable)

      varName = cv.Name
      varType = cv.Type.AsFullName

      If Not DTE.UndoContext.IsOpen Then
        DTE.UndoContext.Open("FMS Inc Make property macro.")
      End If

      If prjItm.FileCodeModel.Language = _
       
"{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}" Then
       
' language is VB
        ' NOTE : change this logic to comply to your standards

        ' make the property
 
       linProp = "Property " + varName + "Property() As " + _

         
varType + System.Environment.NewLine
 
       linProp = linProp + "Get" + System.Environment.NewLine
        linProp = linProp + "Return " + varName + System.Environment.NewLine
        linProp = linProp + "End Get" + System.Environment.NewLine
        linProp = linProp + "Set" + System.Environment.NewLine
        linProp = linProp + varName + " = Value" + System.Environment.NewLine
        linProp = linProp + "End Set" + System.Environment.NewLine
        linProp = linProp + "End Property"

       ElseIf prjItm.FileCodeModel.Language = _
 
     "{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}" Then
       
' language is C#
        ' NOTE : change this logic to comply to your standards

        Const leftBracket As String = "{"
        Const rightBracket As String = "}"

        ' make the property
        linProp = "public " + varType + " " + varName + "Property" + _
          System.Environment.NewLine
        linProp = linProp + leftBracket + System.Environment.NewLine
        linProp = linProp + "get" + System.Environment.NewLine
        linProp = linProp + leftBracket + System.Environment.NewLine
        linProp = linProp + "return " + varName + ";" + System.Environment.NewLine
        linProp = linProp + rightBracket + System.Environment.NewLine
        linProp = linProp + "set" + System.Environment.NewLine
        linProp = linProp + leftBracket + System.Environment.NewLine
        linProp = linProp + varName + " = value;" + System.Environment.NewLine
        linProp = linProp + rightBracket + System.Environment.NewLine
        linProp = linProp + rightBracket + System.Environment.NewLine
      Else
        ' Macro works for VB and C#. It is not applicable for other languages.
        DTE.StatusBar.Text = "Not applicable to this language."
        DTE.StatusBar.Highlight(True)
        Return
      End If

       txtDoc.Selection.EndOfLine()
       txtDoc.Selection.NewLine()

       ' store line number for later
 
     Dim firstPropertyLine As Integer = txtDoc.Selection.ActivePoint.Line

       ' insert string
 
     txtDoc.Selection.Insert(linProp)

       ' select the inserted property block to auto-format
      txtDoc.Selection.MoveToLineAndOffset(firstPropertyLine, 1, True)

      DTE.UndoContext.Close()

      ' format selected inserted block and keep selected
      DTE.ExecuteCommand("Edit.FormatSelection")

    Catch e As System.Exception
      DTE.StatusBar.Text = "Macro failed.."
      DTE.StatusBar.Highlight(True)
    End Try

  End Sub

 

Example

To use this macro, just highlight the variable you want to turn in to a property ("foo" in this case) and run the macro.

  ' VB
  Dim foo As Boolean = False

  // C#
 
private bool foo = false;

Which becomes this code:

  ' VB
 
Dim foo As Boolean = False

  Property fooProperty() As System.Boolean
    Get
         Return foo
 
  End Get
    Set(ByVal Value As System.Boolean)
        foo = Value
    End Set
  End Property

 

   // C#
  private
bool foo = false;
  public System.Boolean fooProperty
  {
    get
    {
      return foo;
    }
    set
    {
      foo = value;
    }
  }

Now this may not be the naming standard which you use for your projects, but that can be easily modified.


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS