Visual Studio tips 2

Karen Payne - Feb 25 '23 - - Dev Community

Visual Studio Generate From Usage feature

This tip focuses on how Visual Studio 2019 or higher provides the ability to create a new class from code a developer has written without actually creating the necessary class.

Example, a developer needs the following class.

public class Person
{
    public Person()
    {
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly BirthDate { get; set; }
    public string Country { get; set; }
    public List<string> PhoneNumbers { get; set; }

}
Enter fullscreen mode Exit fullscreen mode

Visual Studio can create the class by writing out a new Person class as shown below.

Code without Person class

Since the class and it's properties do not exists they have a foreground color of red.

To create the Person class, hover the mouse over Person as shown below.

Hover mouse over new Person

Click on Show potential fixes and the following dialog appears, select Generate new type...

options context menu

In the following dialog, choose access modifier, class or struct, name is disabled.

For location, if there is one project leave as is. For file name, create a new file or place in the current file. Unless there is a good reason for placing the class in the current file, place in a new file.

In this example we are in a standard Console project and want the Person class in a folder named Models which does not exists. To create the Person class under the non-existing Models folder type Models\ in front of Person and press OK. Next, open the new class and update the namespace by appending Models.

In the video, there is a class project without any classes, we want to do what we just did but place the Person class under a folder named Models in the class project.

Video note: 📹 Would had embedded here but there are too many frames.

Now we know there will be other classes that we want an Id Property so lets create an Interface, type : IBase after Person, hover over Person and select Generate interface IBase, in this case it will be created in the same file.

create interface

Add Id property by copy the Id property from the Person class and pasting into the interface.

public class Person : IBase
{
    public Person()
    {
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly BirthDate { get; set; }
    public string Country { get; set; }
    public List<string> PhoneNumbers { get; set; }
    public int Id { get; set; }
}

public interface IBase
{
    public int Id { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Click in the gutter with the mouse cursor on IBase to move to it's own file.

move interface to a new file

Create a new folder named Interfaces, in Solution Explorer drag IBase.cs to the Interface folder, done.

Origins

This functionality comes from TDD (Test Driven development) which is a difficult concept for many developers but given time learning how to write unit test than writing TDD in unit test with proper specifications will extend time to develop an application with the benefits of a better coded application along with using the unit test when sometime breaks, before traversing through code or debugging code run the test methods which very well may point to the problem.

References

See also

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .