Share via


Visual Basic Concepts

Choosing a Default Property or Method for a Class

You can mark the most commonly used public property or method of a class as the default method. This allows the user of a class to invoke the member without naming it.

To set a property or method as the default

  1. On the Tools menu, click 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 Class list, you can scroll through the members in the Members list; the default member will be marked with a small blue circle 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.

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.