DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Visual Studio 2010 Quick Tip #1 - Generate From Usage

Alvin Ashcraft user avatar by
Alvin Ashcraft
·
Apr. 07, 10 · Interview
Like (0)
Save
Tweet
Share
10.64K Views

Join the DZone community and get the full member experience.

Join For Free

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!

 

Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building a Real-Time App With Spring Boot, Cassandra, Pulsar, React, and Hilla
  • Spring Boot, Quarkus, or Micronaut?
  • Real-Time Analytics for IoT
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: