How to: Create and Display an Instance of an Object

You can create object instances and invoke static methods from Class View, Class Designer, the Command window, and the Immediate window, and then display the instance in the Object Test Bench Tool Window.

Object Test Bench works only on the StartUp project in the solution or in a referenced assembly. To set the StartUp project, right-click the project icon in Solution Explorer and then click Set As StartUp project. Only one instance of Object Test Bench exists for every Visual Studio instance. For more information, see How to: Set Startup Projects

Note

Class Designer is available in Visual Studio Professional and Enterprise versions only.

Note

The Create Instance and Invoke Static Method commands are not available for code types other than classes and structs. These menus are also not available for generic or abstract classes and structs.

Note

The statements in main() are executed when Create Instance is called. For example, if you created an instance programmatically in main(), you cannot use Create Instance to create an object with the same name. No error message is issued in this case though.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create an instance of a project type object and display it in the Object Test Bench tool window

  1. In the Class View, right-click the name of the class or struct whose instance you want to create.

  2. Click Create Instance to display the public constructors of the class.

  3. Select the constructor to display the Create Instance Dialog Box.

  4. In the Create Instance dialog box, provide the parameters, and then type the name of the object. The name is not provided by default; you must provide a name.

  5. Check the Object Test Bench tool window for the object shape.

To create an instance of a .NET Framework or Class Library referenced type object not appearing in Class Designer

  1. In Class View, right-click the object type in the Project References node. See How to: Work with Class View for more information.

  2. Select the constructor to display the Create Instance dialog box.

  3. In the Create Instance dialog box, provide the parameters, and then select a name of the object.

  4. Check the Object Test Bench tool window for the object shape.

To create an instance of an object and display it in the Object Test Bench tool window from the Command window

  1. If the Command window is not displayed, on the View menu, click Other Windows, and then click Command Window.

  2. In the Command window, type the command to instantiate the object.

    Note

    You can also call methods that do not have void return types from the Command window, and then instantiate their return values in the Object Test Bench.

  3. Check the Object Test Bench tool window for the object shape.

See Also

Tasks

How to: Display an Instance of a Returned Object

Other Resources

Create and Display Object Test Bench Objects