Visual Studio 2010
Generate From Usage

The Generate From Usage feature enables you to use classes and members before you define them. You can generate a stub for any class, constructor, method, property, field, or enum that you want to use but have not yet defined. You can generate new types and members without leaving your current location in code. This minimizes interruption to your workflow.

Generate From Usage supports programming styles such as test-driven development.

Using Generate From Usage in C#

A wavy underline appears under each undefined identifier. When you rest the mouse pointer on the identifier, an error message appears in a tooltip.

Click the undefined identifier, and a short underline appears under the leftmost character. When you rest the mouse pointer on the short underline, a Smart Tag (a small rectangle) appears. You can also press CTRL+. to display the Smart Tag.

Click the Smart Tag, and the appropriate options appear. Depending on the context, those options can include the following:

  • Generate property stub

  • Generate method stub

  • Generate class

  • Generate new type (for a class, struct, interface, or enum)

You can also right-click the undefined identifier and then click Generate. The appropriate options appear.

Using Generate From Usage in Visual Basic

A wavy underline appears under each undefined identifier, and a short underline appears under the rightmost character. When you rest the mouse pointer on the identifier, an error message appears in a tooltip.

When you rest the mouse pointer on the short underline, a Smart Tag (a small rectangle) appears. You can also press CTRL+. to display the Smart Tag.

Click the Smart Tag, and the appropriate options appear. Depending on the context, those options can include the following:

  • Generate property stub

  • Generate method stub

  • Generate class

  • Generate interface

  • Generate new type (for a Class, Enum, Structure, Interface, or Delegate)

Generating a Property Stub

If code references an undefined property, click the Smart Tag, and then click Generate property stub. The property stub is generated in the appropriate class. The property’s return type is determined from the context.

For example, assume that you generate the InstanceProperty property from the statement in the following code.

Visual Basic
Dim cust As New Customer()
Dim city As String = cust.InstanceProperty
C#
Customer cust = new Customer();
string city = cust.InstanceProperty;

When you generate the property, the following stub is created in the Customer class.

Visual Basic
Property InstanceProperty() As String
C#
public string InstanceProperty { get; set; }

If a property is invoked on a type and not an instance, the generated stub will be a static property (C#) or a shared property (Visual Basic).

For example, imagine that you generate a property from the following statement (assuming that Customer is a class name).

Visual Basic
Dim description As String = Customer.SharedProperty
C#
string description = Customer.StaticProperty;

When you generate the property, the following stub is created in the Customer class.

Visual Basic
Shared Property SharedProperty As String
C#
public static string StaticProperty { get; set; }

If a property is invoked without a qualification (referring to a member of the current type), the generated stub is static if the property is called from a static method. Otherwise, it is an instance property.

For example, assume that you generate a property from the following statement.

Visual Basic
Dim title As String = UnqualifiedProperty
C#
string title = UnqualifiedProperty;

When you generate the property, the following stub is created in the current class.

Visual Basic
Private Property UnqualifiedProperty() As String
C#
public string UnqualifiedProperty { get; set; }
Generating a Method Stub

If code references an undefined method, click the Smart Tag, and then click Generate method stub. The method stub is generated in the appropriate class.

The type of each parameter and the return type are determined from the context. The object type is used when the type cannot be inferred, such as in anonymous types or implicitly typed local variables (variables defined with the keyword var).

The name of each parameter is derived from the names of the arguments that are passed into the method call.

For example, assume that you generate the InstanceMethod method from the statement in the following code.

Visual Basic
Dim cust As New Customer()
Dim itemNumber = 3
Dim itemName = "abc"
cust.InstanceMethod(itemNumber, itemName, 4)
C#
Customer cust = new Customer();
int itemNumber = 3;
string itemName = "abc";
cust.InstanceMethod(itemNumber, itemName, 4);

When you generate the method, the following stub is created in the appropriate class.

Visual Basic
Sub InstanceMethod(ByVal itemNumber As Integer, ByVal itemName As String, ByVal p3 As Integer)
    Throw New NotImplementedException
End Sub
C#
internal void InstanceMethod(int itemNumber, string itemName, int p)
{
    throw new NotImplementedException();
}

If a method is invoked on a type and not an instance, the generated stub is a static method (C#) or a shared method (Visual Basic).

Generating a Method Stub for an Event

In Visual Basic, you can generate an event handler method for an event that is referenced in an AddHandler Statement or a RemoveHandler Statement.

For example, assume that you generate the EventHandler method from either of the following statements in code.

Visual Basic
AddHandler obj.Ev_Event, AddressOf EventHandler
RemoveHandler obj.Ev_Event, AddressOf EventHandler

When you generate the method, the following stub is created in the current class.

Visual Basic
Private Sub EventHandler()
    Throw New NotImplementedException
End Sub
Generating a Constructor Stub

If code references an undefined constructor, click the Smart Tag, and then click Generate constructor stub. The constructor stub is generated in the appropriate class. The type of each parameter is determined from the context.

The names of the constructor’s parameters are examined. If the class has properties that have names that match those parameter names, code is supplied in the constructor to store the argument values for those properties.

For example, assume that you generate a constructor for the Example class from the statement in the following code.

Visual Basic
Dim total As Long = 12
Dim exampleTest As New Example(5, total, Date.Now)
C#
long total = 12;
Example exampleTest = new Example(5, total, System.DateTime.Now);

When you generate the constructor, the following private variables are created in the Example class, if they are not already present.

Visual Basic
Private _p1 As Integer
Private _total As Long
Private _p3 As Date
C#
private int p;
private long total;
private DateTime dateTime;

The following constructor stub is created.

Visual Basic
Sub New(ByVal p1 As Integer, ByVal total As Long, ByVal p3 As Date)
    ' TODO: Complete member initialization 
    _p1 = p1
    _total = total
    _p3 = p3
End Sub
C#
public Example(int p, long total, DateTime dateTime)
{
    // TODO: Complete member initialization
    this.p = p;
    this.total = total;
    this.dateTime = dateTime;
}

You can generate more than one constructor in a class. An additional constructor can be generated if the constructor call has a different number of arguments or different argument types.

In Visual Basic, a constructor can also be generated when you generate a class. See the next section of this topic for more information.

Generating a Class

If code references an undefined class or other type, the Smart Tag displays choices for Generate class and Generate new type.

If you click Generate class, an empty class stub is generated in a new file in the project. The new file is opened in the Code Editor (but not given focus). This is the quickest way to create a new class type with default access modifiers in a new file in the current project.

For example, assume that you generate a class from the following statement.

Visual Basic
Dim cust As Customer
C#
Customer cust;

When you generate the class, the following new class stub is created in a new file in the project.

Visual Basic
Class Customer

End Class
C#
namespace CSharpWindowsApp
{
    class Customer
    {
    }
}

You can also use the Generate class command when an inherited base class is undefined in a class definition.

Generating a Class Together with a Constructor

In Visual Basic, the generated class will include a constructor, if appropriate.

For example, assume that you generate a class from the following statement.

Visual Basic
Dim total = 3
Dim exampleTest As New Example(5, total, Date.Now) With {.Description = "book"}

When you generate the class, the following new class stub is created in a new file in the project. The constructor is generated in the class. A Description property is created for the object initializer that is in the With clause in the previous code.

Visual Basic
Class Example
    Private _p3 As Date
    Private _total As Integer
    Private _p1 As Integer

    Sub New(ByVal p1 As Integer, ByVal total As Integer, ByVal p3 As Date)
        ' TODO: Complete member initialization 
        _p1 = p1
        _total = total
        _p3 = p3
    End Sub

    Property Description As String
End Class
Generating New Types with Additional Options

If code references an undefined class, interface, enum, struct (C#), or structure (Visual Basic), the Smart Tag displays choices for Generate class and Generate new type.

Click Generate new type to open the Generate New Type dialog box, which enables you to choose an access level, a kind, a project location, and a file name.

The Generate new type option provides additional flexibility when you generate a class. You may prefer to put the class in an existing file, or specify the access modifiers, or add the new file to another project in the solution.

The following table shows the choices available in the Generate New Type dialog box.

Option

Choices for C#

Choices for Visual Basic

Access

Default, public, internal, or private.

Default, Friend, or Public.

Kind

Class, struct, interface, or enum.

Class, Structure, Interface, Enum, or Delegate.

Project location

Current project or another project in the solution.

Current project or another project in the solution.

File name

Create new file with file name, or Add to existing file with file name.

Create new file with file name, or Add to existing file with file name.

If the type is generated in a new file, the new file will contain the default set of using directives for a class that is generated by the Add New Item dialog box.

If the type is generated in another project in the solution, a using directive that refers to that project is added to the current file.

Generating an Interface Stub

To generate code from an undefined interface, click Generate new type in the Smart Tag. The Generate New Type dialog box opens. In the Kind list, click interface. In Visual Basic, you can also click Generate Interface in the Smart Tag.

For example, assume that you generate the IAuto interface from the statement in the following code.

Visual Basic
Public Class Sedan : Implements IAuto
End Class
C#
public class Sedan : IAuto
{
}

When you generate the interface, the following stub is created.

Visual Basic
Interface IAuto

End Interface
C#
interface IAuto
{
}
Generating a Delegate Stub

To generate code from an undefined delegate in Visual Basic, click Generate new type in the Smart Tag. The Generate New Type dialog box opens. In the Kind list, click Delegate.

For example, assume that you generate the MathOperator delegate from the statement in the following code.

Visual Basic
Dim delInstance As New MathOperator(AddressOf AddNumbers)

When you generate the delegate, the following stub is created.

Visual Basic
Delegate Sub MathOperator()
See Also

Tasks

Other Resources

Page view tracker