Easy way to generate Getters and Setters in Visual Studio

Is there a way to generate getters and setters in Visual Studio? I try to Alt+ R, Fand I get the following:

public String Denomination
{
    get { return denomination; }
    set { denomination = value; }
}

      

I want too:

public String getDenomination()
{
    return Denomination;
}

public void setDenomination(String Denomination)
{
    this.Denomination = Denomination;
}

      

is there a way to do this?

+3


source to share


3 answers


You can use the code snippet prop

to create automatic properties.

Enter prop

and press Tab

. Then you can change the Type and name of the property.



In your simple case, where no additional logic is needed, there is no need for fallback fields.

+6


source


I don't think there is a built-in way to do this out of the box using Visual Studio, but they do provide you with the ability to add this functionality.



What you need to do is create a snippet of code that creates these two methods and adds the snippet to your folder %USERPROFILE%\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets

. After that, you will be able to enter the snippet name and click tab

and it will fill in the text you are looking for.

+3


source


This is just an opinion , but I'm not a big fan of properties since I started with Java development and then switched to C #.

I know developers who love them, and I know those who hate them, but if your team, for example, wants to use getter and setter instead of properties, then this snippet might be of interest to you.

I changed the one that generates properties to suit my needs, but might work for you as well.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>getset</Title>
            <Shortcut>getset</Shortcut>
            <Description>Code snippet for Getter and Setter</Description>
            <Author>bongo</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>object type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>GSName</ID>
                    <ToolTip>Getter Setter name</ToolTip>
                    <Default>MyMethod</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>The variable backing this Getter Setter</ToolTip>
                    <Default>myVar</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[private $type$ $field$;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    public void Set$GSName$($type$ value)
    {
        $field$ = value;
    }       

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public $type$ Get$GSName$()
    {
        return $field$;
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

      

if you add it like Scott Chamerlein said or using the Code Snapshot command in Visual Studio under the Tools tab, you can type getet and then click the tab in Visual Studio and it will generate this:

private int myVar;

/// <summary>
/// 
/// </summary>
/// <param name="value"></param>
public void SetMyMethod(int value)
{
    myVar = value;
}

/// <summary>
/// 
/// </summary>
/// <returns></returns>
public int GetMyMethod()
{
    return myVar;
}

      

+1


source







All Articles