Over a million developers have joined DZone.

Visual Studio 2010 Quick Tip #1 - Generate From Usage

·

One of the new features available in the Visual Studio 2010 editor is Generate From Usage. This feature provides the ability to use a type member in your code that does not yet exist, and then automatically generate a stub for that member. Visual Studio determines type and signature information for that new stub based upon the usage. Here are a few examples of the feature in action.

 

Generate Property Stub

1. Create a TestUsages method and set the value of a non-existent property called DZoneQuality.

2. Hover over and expand the smart tag that appears over the property usage. A dropdown menu of options appears. You can have the editor create stubs for a property or a field.

3. An auto-property for DZoneQuality has been created as a string based on its usage.

 

Generate Method Stub

1. Call a SetQuality method that takes a single parameter, passing the DZoneQuality property we just generated.

2. Hover over and expand the smart tag that appears over the method usage. A dropdown menu with a single option appears. Select the menu item to create the new method stub.

 

3. A new SetQuality method is created with a single string parameter and a void return type, based on its usage. The method stub throws a NotImplementedException.

 

Generate Class

1. Enter code to create a new instance of a non-existent class called SiteRating. We’ll pass our DZoneQuality property as a parameter to the constructor to see how smart the class generator is.

2. Hover over the smart tag and click to display the menu options. For this example, we’re going to choose the standard Generate class option.

3. In the Solution Explorer, we can see that a new SiteRating.cs file has been added to our project.

 

4. Open the file, and we can see that the class created does not contain our overloaded constructor with a string parameter. Only the default constructor will be available, so our code is not going to compile at this point.

 

5. Could we have generated a class with the correct constructor if we had chosen the ‘Generate new type…” option? Let’s take a look. As you can see on the dialog that appears, there’s no option to create constructors here either. There are some other useful options available though. We will have to go back to our TestUsages() method.

6. Now when we open the smart tag, we have an option to generate the correct constructor.

 

7. The editor adds the constructor that takes a string parameter. In addition, a new property was added to the SiteRating class and the constructor sets the value of that property from the constructor’s parameter. Not bad.

 

 

Enjoy this new feature!

 

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}