Making a Property or Method the Default

See Also

You can give objects created from your classes default properties, like the default properties of objects provided by Visual Basic. The best candidate for default member is the one you use most often.

To set a property or method as the default

  1. On the Tools menu, select Procedure Attributes to open the Procedure Attributes dialog box.

  2. Click Advanced to expand the Procedure Attributes dialog box.

  3. In the Name box, select the property or method that is currently the default for the class. If the class does not currently have a default member, skip to step 5.

    Note   You can use the Object Browser to find out what the current default member of a class is. When you select the class in the Classes list, you can scroll through the members in the Members list; the default member will be marked with a small blue globe beside its icon.

  4. In the Procedure ID box, select None to remove the default status of the property or method.

  5. In the Name box, select the property or method you want to be the new default.

  6. In the Procedure ID box, select (Default), then click OK.

Important   A class can have only one default member. If a property or method is already marked as the default, you must reset its procedure ID to None before making another property or method the default. No compile errors will occur if two members are marked as default, but there is no way to predict which one Visual Basic will pick as the default.

You can also open the Procedure Attributes dialog box from the Object Browser. This is convenient when you're changing the default member of a class, because it allows you to locate the existing default member quickly.

To change a default property using the Object Browser

  1. Press F2 to open the Object Browser.

  2. In the Classes list, select the class whose default you want to change.

  3. In the Members list, right-click the member with the small blue globe beside its icon to open the context menu. Click Properties to show the Property Attributes dialog box.

  4. Click Advanced to expand the Procedure Attributes dialog box.

  5. In the Procedure ID box, select None to remove the default status of the property or method, then click OK.

  6. In the Members list, right-click the member you want to be the new default to open the context menu. Click Properties to show the Property Attributes dialog box.

  7. Click Advanced to expand the Procedure Attributes dialog box.

  8. In the Procedure ID box, select (Default), then click OK.

Note   You cannot use the Procedure Attributes dialog box to change the default member of a class provided by Visual Basic.

Fixing Defaults You Have Accidentally Made Private or Friend

The Procedure Attributes dialog box only allows you to select public properties and methods as the default for a class. If you make a public property or method the default for a class, and later change the declaration to Private or Friend, the property or method may continue to behave as if it were still declared Public.

To correct this problem, you must make the property or method Public again, because the Procedure Attributes dialog box will not show procedures declared Private and Friend. Once you have changed the declaration to Public, you can use the Procedure Attributes dialog to remove the Default attribute. You can then change the declaration back to Friend or Private.