Udostępnij za pośrednictwem


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-first 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.

To display the appropriate options, you can use one of the following procedures:

  • Click the undefined identifier. A short underline appears under the leftmost character. Rest the mouse pointer on the short underline, and a smart tag (an icon) appears. Click the smart tag.

  • Click the undefined identifier, and then press CTRL+. (period).

  • Right-click the undefined identifier, and then click Generate.

The options that appear can include the following:

  • Generate property stub

  • Generate field stub

  • Generate method stub

  • Generate class

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

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.

To display the appropriate options, you can use one of the following procedures:

  • Rest the mouse pointer on the undefined identifier. A smart tag (an icon) appears. Click the smart tag.

  • Click the undefined identifier, and then press CTRL+. (period).

  • In the Error List window, double-click the corresponding error row.

The options that appear can include the following:

  • Generate property stub

  • Generate field stub

  • Generate method stub

  • Generate class

  • Generate interface

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

Generating a Property Stub

If code references an undefined property, 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.

Dim cust As New Customer()
Dim city As String = cust.InstanceProperty
Customer cust = new Customer();
string city = cust.InstanceProperty;

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

Property InstanceProperty() As String
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).

Dim description As String = Customer.SharedProperty
string description = Customer.StaticProperty;

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

Shared Property SharedProperty As String
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.

Dim title As String = UnqualifiedProperty
string title = UnqualifiedProperty;

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

Private Property UnqualifiedProperty() As String
public string UnqualifiedProperty { get; set; }

Generating a Method Stub

If code references an undefined method, 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.

Dim cust As New Customer()
Dim itemNumber = 3
Dim itemName = "abc"
cust.InstanceMethod(itemNumber, itemName, 4)
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.

Sub InstanceMethod(ByVal itemNumber As Integer, ByVal itemName As String, ByVal p3 As Integer)
    Throw New NotImplementedException
End Sub
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.

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.

Private Sub EventHandler()
    Throw New NotImplementedException
End Sub

Generating a Constructor Stub

If code references an undefined constructor, 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.

Dim total As Long = 12
Dim exampleTest As New Example(5, total, Date.Now)
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.

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

The following constructor stub is created.

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
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, choices appear 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.

Dim cust As Customer
Customer cust;

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

Class Customer

End Class
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.

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.

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), choices appear for Generate class and Generate new type. This is shown in the following illustrations.

Visual Basic

Smart Tag Context Menu in Visual Basic

Visual C#

Smart Tag Context Menu in C#

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 illustration shows the Generate New Type dialog box.

Generate New Type dialog box

Generate New Type dialog box

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, Delegate, or Module.

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. The Generate New Type dialog box opens. In the Kind list, click interface. In Visual Basic, you can also click Generate Interface.

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

Public Class Sedan : Implements IAuto
End Class
public class Sedan : IAuto
{
}

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

Interface IAuto

End Interface
interface IAuto
{
}

Generating a Delegate Stub

To generate code from an undefined delegate in Visual Basic, click Generate new type. 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.

Dim delInstance As New MathOperator(AddressOf AddNumbers)

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

Delegate Sub MathOperator()

IntelliSense Suggestion Mode

IntelliSense provides two alternatives for statement completion: completion mode and suggestion mode. Use suggestion mode for situations in which classes and members are used before they are defined.

In IntelliSense suggestion mode, when you type in the Code Editor and then commit the entry, the text you typed is inserted into the code. When you commit an entry in IntelliSense completion mode, the selected entry in the members list is inserted into the code.

When an IntelliSense window is open, you can press CTRL+ALT+SPACEBAR to switch between completion mode and suggestion mode.

For more information, see List Members.

See Also

Tasks

Walkthrough: Test-First Support with the Generate From Usage Feature

Other Resources

Coding Aids

Automatic Code Generation