Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    property code snippet

    Just went to a class at TechEd on C# IDE enhancements for VS 2005 and loved it.  I had already seen about 80% of it being on the Whidbey Alpha, but there were some more really cool things in there.  Again I was reminded seeing the property generation code snippet that I think it's silly to have to specify a field name AND a property name.

    Since code snippets are just xml files, you can go in and change it yourself.  Here's how I changed mine...
    (located in “C:\Program Files\Microsoft Visual Studio 8\VC#\Expansions\1033\Expansions\property.xml” taken from the Code node in the xml file)

    private $type$ _$property$

    public $type $property$
    {
         get { return _$property$;}
         set { _$property$ = value;}
    }
    $end$

    So now with this, the snippet will be smart in the IDE and only ask you to enter the type and the name of the backing field, instead of also asking you for the name of the property.  If you don't like it, change it...very cool!  :)

    UPDATE: I fixed the problems with the code (I was talking to someone while posting  :P) and also talked to someone about how to possibly dump backing variables into a region, but it sounds like at this point it's only possible by doing a plug-in or something, accessing the code model that way.

    Posted: May 27 2004, 03:16 PM by HumanCompiler | with 7 comment(s)
    Filed under:

    Comments

    Joe said:

    Hey man I think your example is cool but wrong. I think you just made a minor error.

    If you use that snipet you will probly get stuck in an infinte loop.

    sol.
    private $type$ $property$
    {
    get { return _$property$;}
    set { _$property$ = value;}
    }
    $end$

    # May 27, 2004 8:46 PM

    HumanCompiler said:

    oops...typo...thanks...fixed!
    # May 27, 2004 9:07 PM

    Thomas Freudenberg said:

    Hmm, I would prefer to have the field in Camel case and the property in Pascal case, but I don't think that this is possible.
    # May 28, 2004 6:24 AM

    Frans Bouma said:

    I'm with Thomas, the property should be CaMel cased.

    Here's a macro to do it today in VS.NET 2003:

    ''' Creates a property.
    ''' format: type name has to be present at the current line.
    ''' Uses caMel casing.
    Sub CreateProperty()
    Dim typeName, propertyName, memberName, nameRead As String
    Dim ts As TextSelection
    Dim sb As New StringBuilder
    Dim boolIsOpened As Boolean

    Try
    ts = DTE.ActiveDocument.Selection
    If (ts.IsEmpty) Then
    ts.SelectLine()
    End If

    typeName = (ts.Text.Split(" ")(0)).Trim()
    nameRead = ((ts.Text.Split(" ")(1))).Trim()
    propertyName = nameRead.Substring(0, 1).ToUpper & nameRead.Substring(1)
    memberName = "_" + nameRead.Substring(0, 1).ToLower() & nameRead.Substring(1)

    ' actual property
    sb.Append("/// <summary>" & Environment.NewLine)
    sb.AppendFormat("/// Gets / sets {0}{1}", nameRead, Environment.NewLine)
    sb.Append("/// </summary>" & Environment.NewLine)
    sb.Append("public " + typeName + " " + propertyName)
    sb.Append(vbCrLf)
    sb.Append("{")
    sb.Append(vbCrLf)

    ' get
    sb.Append("get")
    sb.Append(vbCrLf)
    sb.Append("{")
    sb.Append(vbCrLf)
    sb.AppendFormat("return {0};", memberName)
    sb.Append(vbCrLf)
    sb.Append("}")
    sb.Append(vbCrLf)

    ' set
    sb.Append("set")
    sb.Append(vbCrLf)
    sb.Append("{")
    sb.Append(vbCrLf)
    sb.AppendFormat("{0} = value;", memberName)
    sb.Append(vbCrLf)
    sb.Append("}")
    sb.Append(vbCrLf)
    sb.Append("}")
    sb.Append(vbCrLf)
    sb.Append(vbCrLf)

    'Check to see if UndoContext object is already open.
    If DTE.UndoContext.IsOpen = False Then
    'Open the UndoContext object to track changes.
    Call DTE.UndoContext.Open("CreateProperty " & propertyName, False)
    boolIsOpened = True
    End If

    ' Replace the text
    ts.Delete()
    ts.Insert(sb.ToString(), vsInsertFlags.vsInsertFlagsInsertAtStart)
    Finally

    'If UndoContext was already open, don't close it.
    If boolIsOpened = True Then
    'Close the UndoContext object to commit the changes.
    DTE.UndoContext.Close()
    ' Format the Selection
    ts.SmartFormat()
    ts.MoveToPoint(ts.BottomPoint.CreateEditPoint())
    ts.LineUp()
    End If
    End Try
    End Sub


    Simply do this:

    int foo
    then at that line hit the key combi which executes the macro and you'll get:

    /// <summary>
    /// Gets / sets foo
    /// </summary>
    public int Foo
    {
    get
    {
    return _foo;
    }
    set
    {
    _foo = value;
    }
    }

    # May 28, 2004 6:40 AM

    HumanCompiler said:

    Thomas, I talked with Joe N on the C# team about functions in snippets. He said it's something he's been pushing to get in there, but doubts it will for VS 2005.

    Frans, that's fine...there are bunches of macros out there for that, but this entry is talking about code snippets in VS 2005. And in the future, please post code as a new entry and link to me, not in the comment...thanks. Also, that is your opinion that it should be cased like that...not a fact that it "should" be that way. ;)
    # May 28, 2004 5:10 PM

    shelby said:

    You can improve the snippet a little by adding the auto documentation as follows

    private $type$ $property$;

    /// <summary>

         /// gets/sets the $property$.

         /// </summary>

         /// <value>The $property$</value>

    private $type$ $property$

    {

    get { return _$property$;}

    set { _$property$ = value;}

    }

    $end$

    # April 3, 2007 7:47 AM

    PMBjornerud said:

    Autodocumentation is meaningless and only makes code harder to read maintain.

    It states the obvious, but the point of documentation is to explain the non-obvious.

    # September 18, 2007 10:41 AM