How to: Declare and Call a Default Property in Visual Basic
A default property is a class or structure property that your code can access without specifying it. When calling code names a class or structure but not a property, and the context allows access to a property, Visual Basic resolves the access to that class or structure's default property if one exists.
A class or structure can have at most one default property. However, you can overload a default property and have more than one version of it.
For more information, see.
To declare a default property
Declare the property in the normal way. Do not specify the Shared or Private keyword.
Include the Default keyword in the property declaration.
Specify at least one parameter for the property. You cannot define a default property that does not take at least one argument.
To call a default property
Declare a variable of the containing class or structure type.
Use the variable name alone in an expression where you would normally include the property name.
Follow the variable name with an argument list in parentheses. A default property must take at least one argument.
To retrieve the default property value, use the variable name, with an argument list, in an expression or following the equal (=) sign in an assignment statement.
To set the default property value, use the variable name, with an argument list, on the left side of an assignment statement.
You can always specify the default property name together with the variable name, just as you would do to access any other property.
The following example declares a default property on a class.
Public Class class1 Private myStrings() As String Sub New(ByVal size As Integer) ReDim myStrings(size) End Sub Default Property myProperty(ByVal index As Integer) As String Get ' The Get property procedure is called when the value ' of the property is retrieved. Return myStrings(index) End Get Set(ByVal Value As String) ' The Set property procedure is called when the value ' of the property is modified. ' The value to be assigned is passed in the argument ' to Set. myStrings(index) = Value End Set End Property End Class
This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Visual Basic Language. For more information, see.
The following example demonstrates how to call the default property myProperty on class class1. The three assignment statements store values in myProperty, and thecall reads the values.
Sub Test() Dim x As New class1(3) x(1) = "Hello" x(2) = " " x(3) = "World" MsgBox(x(1) & x(2) & x(3)) End Sub
The most common use of a default property is theon various collection classes.
Default properties can result in a small reduction in source code-characters, but they can make your code more difficult to read. If the calling code is not familiar with your class or structure, when it makes a reference to the class or structure name it cannot be certain whether that reference accesses the class or structure itself, or a default property. This can lead to compiler errors or subtle run-time logic errors.
You can somewhat reduce the chance of default property errors by always using theto set compiler type checking to On.
If you are planning to use a predefined class or structure in your code, you must determine whether it has a default property, and if so, what its name is.
Because of these disadvantages, you should consider not defining default properties. For code readability, you should also consider always referring to all properties explicitly, even default properties.