3

I'm working in VS2008 and C#, and I'm looking for a (free) code generator tool to generate a property with getter and setter, as well as the backing private field to go with. The template thingy in VS does not make the field to go with it. Just looking for something a little bit better.

I once saw a web site where you could build this code, then cust-and-paste it from the web page to your code.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
MattSlay
  • 9,115
  • 5
  • 43
  • 52
  • 1
    Maybe you should try CodeRush Express: http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/ – Oliver Apr 07 '10 at 10:54

4 Answers4

6

You can create custom snippets to do pretty much anything you want. Here is one I used in VS2005 for creating properties with backing fields:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>prop</Title>
                    <!-- the shortcut below will show in your intellisense 
                         window - set it to whatever you wish -->
            <Shortcut>_prop</Shortcut>
            <Description>Code snippet for a property</Description>
            <Author>Andrew</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <Default>String</Default>
                    <ToolTip>property type</ToolTip>
                </Literal>
                <Literal>
                    <ID>pname</ID>
                    <Default>_name</Default>
                    <ToolTip>private field name</ToolTip>
                </Literal>
                <Literal>
                    <ID>name</ID>
                    <Default>Name</Default>
                    <ToolTip>property name</ToolTip>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                    <![CDATA[$type$ $pname$;

            public $type$ $name$
            {
                get { return this.$pname$; }
                set { this.$pname$ = value; }
            }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Save this in a file called whatever.snippet in this location:

"C:\Documents and Settings\<YOU>\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets"

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • It works ok, but its all in-line: bool _Visible; public bool Visible { get { return this._Visible; } set { this._Visible = value; } } How can I add Carriage Returns to expand it all out? – MattSlay Feb 27 '09 at 16:57
  • 1
    Since the newlines are in the CDATA section they ought to be preserved. Very strange - I see the newlines on my machine :) – Andrew Hare Feb 27 '09 at 17:17
  • Ahhh.. I see. It got goofed up during cut-and-paste from the web page to the new XML file I created. I fixed it, and now it works! Awsesome help. Thanks. – MattSlay Feb 27 '09 at 17:32
0

I looked around on StackOverlfow before I posted to try to find the answer, because I was sure this was already addressed before. I hated to post, but I did look first, I promise.

I kept looking more, a I found this other helpful thread here:

How to generate getters and setters in Visual Studio?

Community
  • 1
  • 1
MattSlay
  • 9,115
  • 5
  • 43
  • 52
0

With CodeSmith is just a click away. Sometimes is better to buy the tool, instead of reinventing the wheel

<%-- 
Name: Database Table Properties
Authors: Paul Welter , Yordan Georgiev
Description: Create a list of properties from a database table with a region for each prop
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>

#region <%= StringUtil.ToPascalCase(column.Name) %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%= StringUtil.ToPascalCase(column.Name) %>;

public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %>
{
    get { return _<%= StringUtil.ToPascalCase(column.Name) %>; }
    set { _<%= StringUtil.ToPascalCase(column.Name) %> = value; }
}
#endregion <%= StringUtil.ToPascalCase(column.Name) %>
<% } %>
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
0

As I am sure you already know, the C# compiler in VS 2008 will automatically generate getters and setters for properties when compiling. This even works for .NET 2.0 applications since it is all done in the compiler.

That means you can do this:

public int Number { get; set; }

Instead of

private int _number;
public int Number
{
get { return this._number; }
set { this._number = value; }
}
Navaar
  • 570
  • 4
  • 5