แก้ไข

แชร์ผ่าน


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

To declare a default property

  1. Declare the property in the normal way. Do not specify the Shared or Private keyword.

  2. Include the Default keyword in the property declaration.

  3. Specify at least one parameter for the property. You cannot define a default property that does not take at least one argument.

    Default Property myProperty(ByVal index As Integer) As String
    

To call a default property

  1. Declare a variable of the containing class or structure type.

    Dim x As New class1(3)
    
  2. Use the variable name alone in an expression where you would normally include the property name.

    MsgBox(x)
    
  3. Follow the variable name with an argument list in parentheses. A default property must take at least one argument.

    MsgBox(x(1))
    
  4. 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.

    MsgBox(x(1) & x(2) & x(3))
    
  5. To set the default property value, use the variable name, with an argument list, on the left side of an assignment statement.

    x(1) = "Hello"
    x(2) = " "
    x(3) = "World"
    
  6. You can always specify the default property name together with the variable name, just as you would do to access any other property.

    x.myProperty(1) = "Hello"
    x.myProperty(2) = " "
    x.myProperty(3) = "World"
    

Example 1

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

Example 2

The following example demonstrates how to call the default property myProperty on class class1. The three assignment statements store values in myProperty, and the MsgBox call 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 the Item[] property on various collection classes.

Robust Programming

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 the Option Strict Statement to 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.

See also