The Best of Visual Studio 2010: Using Generate from Usage

Tips Search

If you like the tips then you'll love the book!

Order your copy today:
http://www.amazon.com/gp/product/0735649928

The Best of Visual Studio 2010: Using Generate from Usage

  • Comments 6

Menu:  Edit -> IntelliSense -> Generate -> New Type
Command:  Edit.GenerateNewType; EditorContextMenus.CodeWindow.Generate.GenerateNewType
Versions:  2010
Published:  1/21/2010
Code:  vstipEdit0011

 

I'm a big fan of Test Driven Development (TDD) so I absolutely love this tip as it is a big step toward enabling the TDD folks in Visual Studio.  The idea behind it is simple, it allows you to use classes and members before you define them.  There are some slight differences between the C# and VB implementations so let's take a look:

VB

1. Start by using a class that you haven't created yet:
clip_image002

2. Obviously, you will get an error.
clip_image004

3. But wait.  When you click the Error Correction Options you get something new:
clip_image006

4. Click on "Generate 'Class Dollar' " and you get a new file called Dollar.vb with a class stub inside:
clip_image008

5. The error is gone and you are ready to start using the class.  You basically repeat the process for members that you create for the new class as you use them.

 

 

 

C#

1. Start by using a class that doesn't exist:
clip_image010

 

2. Here you have a couple of options.  You can right-click and choose Generate -> Class or you can click on the Smart Tag and choose "Generate class for 'Dollar' ".  They both do the same thing and I show both options here:
clip_image012
clip_image014

 

3. You get a new file called Dollar.cs with a class stub inside:
clip_image016

 

4. The error is gone and you are ready to start using the class.  You basically repeat the process for members that you create for the new class as you use them.

 

 

 

SPECIAL NOTE:  Once you get the hang of this and actually start using this feature for TDD activities you will NOT use the examples I gave but will instead choose the "Generate New Type" option.  The dialog is the same for VB and C# and looks like this:

clip_image018

 

Notice you have the ability to set Access, Kind, and-most importantly, Location.  It's the Location option that TDD folks will use to put the classes into the proper project outside of their test project.

  • Nice time saver! Feels like a great evolution from class wizards that require you to put all the properties and functions in up front.

    Really looking forward to the new refactoring stuff in VS2010 - it gets better with every release.

  • That does seem like a neat idea.

    I'd love to see it for C++ as well one day; would reduce a lot of drudge work, assuming it works out the argument types for methods based on what you call them with. Can't have everything, though. :)

  • Scott -- yeah 2010 absolutely rocks!  I just got done doing a gig showing the new features to a crowd today and always love the response when I show this feature.

    Leo -- it DOES do method argument types.  Should I do a post on that?

    Z

  • Sure, I think it would be worth pointing out, even if just as a little "by the way..." post that refered back to the original post for anyone who didn't automatically think of it. Up to you though, of course. :)

    At least, I know that's the aspect of it which I would find the most useful & time-saving. I often write out a call to a non-existent function with arguments from the (existent :) ) function I'm in and then have to go all over the source file working out exaclty what the argument types should be.

    (Often I'm lifting part of a function into a standalone function, which I believe the C# refactoring support can automate even further, though I've not had a chance to use it. I've done quite a bit of C# but was suck using an old framework/VS version at the time due to constraints from other projects that had to interop with mine.)

  • This is really cool and so very useful.  I had a play with this in the RC issue and when generating a type and creating a file you needed to prefix the file name with the path to the folder in the target project.  In the final release the dropdown now offers the folders from the target project, but on selecting from the dropdown you lose the default filename and have to re-type it (or a new one of your choice).  This is a very minor irritation for such cool functionality.

  • This feature SUCKS!!!!!!!!!!!!!!!!!!!!!

    It seems like I spend 10% of my time removing classes the IDE generated for me.  And that does not consider the time spent debugging phantom classes that replace real classes defined in another assembly or namespace.  

    I am trying to add a using namespace but whenever the dll that contains the class or interface is not available or I mistyped the class name, this new class gets generated.

    A little more thinking about side effects of these commands for those of us who try to develop quickly would be helpful.

    At least provide a way to shut this off in a future version.

Page 1 of 1 (6 items)
Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post