Compartilhar via


Como: Usar uma classe que define operadores (Visual Basic)

If you are using a class or structure that defines its own operators, you can access those operators from Visual Basic.

Defining an operator on a class or structure is also called overloading the operator.

Exemplo

The following example accesses the SQL structure SqlString, which defines the conversion operators (Função CType (Visual Basic)) in both directions between a SQL string and a Visual Basic string. Use CType(SQL string expression, String) to convert a SQL string to a Visual Basic string, and CType(Visual Basic string expression, SqlString) to convert in the other direction.

' Insert the following line at the beginning of your source file.
Imports System.Data.SqlTypes
Public Sub setJobString(ByVal g As Integer)
    Dim title As String
    Dim jobTitle As System.Data.SqlTypes.SqlString
    Select Case g
        Case 1
            title = "President"
        Case 2
            title = "Vice President"
        Case 3
            title = "Director"
        Case 4
            title = "Manager"
        Case Else
            title = "Worker"
    End Select
    jobTitle = CType(title, SqlString)
    MsgBox("Group " & CStr(g) & " generates title """ &
          CType(jobTitle, String) & """")
End Sub

The SqlString structure defines a conversion operator (Função CType (Visual Basic)) from String to SqlString and another from SqlString to String. A demonstrativo que atribui title para jobTitle usa o operadorfirst e o MsgBoxchamada defunção usa o segundo.

Compilando o código

Be sure the class or structure you are using defines the operator you want to use. Do not assume that the class or structure has defined every operator available for overloading. For a list of available operators, see Instrução Operator.

Include the appropriate Imports statement for the SQL string at the beginning of your source file (in this case System.Data.SqlTypes).

Seu projeto deve ter referências a System. Data e o sistema.OXML.

Consulte também

Tarefas

Como: Definir um operador (Visual Basic)

Como: Definir um operador de conversão (Visual Basic)

Como: Chamar um procedimento de operador (Visual Basic)

Como: Declarar uma estrutura (Visual Basic)

Referência

Expansão (Visual Basic)

Restrição (Visual Basic)

Instrução Structure

Conceitos

Procedimentos de operador (Visual Basic)

Conversões explícitas e implícitas (Visual Basic)

Conversões de expansão e restrição (Visual Basic)