Compartilhar via


Como: declarar e chamar uma propriedade padrão em 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 Padrão (Visual Basic).

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"
    

Exemplo

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

Este exemplo de código também está disponível como um trecho de código IntelliSense. In the code snippet picker, it is located in Visual Basic Language. For more information, see Como: Inserir trechos de código de IntelliSense.

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

O uso mais comum de uma propriedade do padrão é o Itemdepropriedade em várias classes de coleção .

Programação robusta

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 Opção declaração estrito 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.

Consulte também

Tarefas

Como: Criar uma propriedade (Visual Basic)

Como: Declarar uma propriedade com níveis de acesso mistos (Visual Basic)

Como: Chamar um procedimento de propriedade (Visual Basic)

Como: Inserir um valor em uma propriedade (Visual Basic)

Como: Obter um valor de uma propriedade (Visual Basic)

Referência

Propriedade declaração

Padrão (Visual Basic)

Conceitos

Procedimentos de propriedade (Visual Basic)

Parâmetros e argumentos de procedimento (Visual Basic)

Diferenças entre variáveis e propriedades em Visual Basic