0

Can VS 2012 professional generate a class constructor without any addons?

I can't believe I can't find the option to do this, and if it can't do it, it truly is a conspiracy :)

I have my class defined:

public class User
{
   public int Id {get;set;}
   public string Name {get;set;}
}

Now is there a shortcut that will generate the constructor, toString() method etc?

lennon310
  • 12,503
  • 11
  • 43
  • 61
public static
  • 12,702
  • 26
  • 66
  • 86
  • Generate as in, automatically? – BradleyDotNET Oct 02 '14 at 19:43
  • Re-sharper can do it... but visual studio has no such option. –  Oct 02 '14 at 19:44
  • @BradleyDotNET yes, I have updated my question to clarify what I am asking. – public static Oct 02 '14 at 19:45
  • @Hypenate Yes I know resharper can do it, but vs.net is so advanced and $ I am shocked it doesn't have basic code generation w/o any addons. – public static Oct 02 '14 at 19:45
  • there are a *few* code generation shortcuts i know of and use often. All of which you hit tab after: ctor (contructor), prop, propg... there are many more but those are the ones i use on the regular – Kritner Oct 02 '14 at 19:46
  • 1
    @Kritner Those are called *snippets* and are probably the best choice here. – BradleyDotNET Oct 02 '14 at 19:47
  • ahh snippets neat. Yeah found a list of them here: http://msdn.microsoft.com/en-us/library/z41h7fat(v=vs.90).aspx – Kritner Oct 02 '14 at 19:48
  • @Kritner it just created an empty constructor for me, no option to init all the properties? – public static Oct 02 '14 at 19:54
  • possible duplicate of [Shortcut for creating constructor with variables (C# VS2010)](http://stackoverflow.com/questions/8893979/shortcut-for-creating-constructor-with-variables-c-vs2010) – AFract Oct 02 '14 at 19:57

3 Answers3

3

If you need a default constructor then there is a code snippet ctor for it.


But if you need a constructor with parameters then in you code write:

User user = new User(2, "Name");

This will be an error, since there is no constructor with two parameters, but you will get a blue under line if you hover your mouse over User in new User. Click on that or put your cursor on User and press Ctrl + . It will give you an option to generate constructor like:

enter image description here

That will give you a constructor with fields like:

public class User
{
    private int p1;
    private string p2;

    public User(int p1, string p2)
    {
        // TODO: Complete member initialization
        this.p1 = p1;
        this.p2 = p2;
    }
    public int Id { get; set; }
    public string Name { get; set; }
}

Then you have to go in and remove p1, p2 and point to Id, Name and also rename parameters in constructor. That is probably the best you can do with only Visual studio.

See: Generate From Usage - MSDN (thanks to @Peter Ritchie)


Consider installing Re-Sharper it has much better option for generating not only constructor but other very helpful code.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • @PeterRitchie, that is very useful link. – Habib Oct 02 '14 at 20:12
  • Thanks but it is as if the designs of vs.net don't actually code. I don't want to write out that line just to generate a constructor. Sure its nice to have that option also, but while I am creating a class I want to generate a full constructor! – public static Oct 02 '14 at 20:12
  • Consider installing Resharper? Tell that to my boss who has to pay $xxx for it lol. – public static Oct 02 '14 at 20:13
  • 2
    @publicstatic, Not directly related but show your boss [The Programmer's Bill of Rights - Coding Horror](http://blog.codinghorror.com/the-programmers-bill-of-rights/) and think about switching companies then :P – Habib Oct 02 '14 at 20:16
  • 1
    @Habib are you hiring? :) My boss may show me the door. – public static Oct 02 '14 at 20:18
0

snippets are built in and you can add your own. ctor will make the constructor, I don't think there is anything for ToString, though you can just type override, pick it from the list and it will stub it out.

Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20
-1

"ctor" snippet generates a constructor, but without parameters. To get them, you should unfortunately use (free or not) addons. This question has been already discussed, see Shortcut for creating constructor with variables (C# VS2010) for example.

I think there is no direct mean to generate a paramerized constructor in a bare Visual Studio.

Edit : I should probably have added after my last sentence "... which strictly applies to properties and fields currently defined in class". But yes, following Peter's advice it is possible to generate a kind of parameterized constructor.

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70
  • 1
    Another question details that it *is* possible, and it's documented for as far back as 2010 http://msdn.microsoft.com/en-us/library/dd409796(v=vs.100).aspx – Peter Ritchie Oct 02 '14 at 20:08
  • @PeterRitchie Not agree. "Generate for usage" is not strictly "Generate in place constructor" : the parameters order and names would not match with existing props and fields in class ! It's nice to mention it since it can be helpful, but this solution is far to be as efficient as R# ctor generation for example. – AFract Oct 02 '14 at 20:13
  • And "ctor" was mentioned. OP shows us some fields present in class so I guessed his goal was to generate a constructor against them... – AFract Oct 02 '14 at 20:22