Partial (Visual Basic)
Indicates that a class or structure declaration is a partial definition of the class or structure.
Você pode dividir a definição de uma classe ou estrutura entre várias declarações usando a Partial palavra-chave. Você pode usar quantas declarações parcial , como você deseja nos arquivos de fonte de diferentes quantas conforme desejado. However, all the declarations must be in the same assembly and the same namespace.
Observação |
---|
Visual Basic 2008apresenta métodosparcial para uso em classes parcial . For more information, see Métodos parciais (Visual Basic) and Instrução Sub (Visual Basic). |
[ <attrlist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] _
Partial { Class | Structure } name [ (Of typelist) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ variabledeclarations ]
[ proceduredeclarations ]
{ End Class | End Structure }
Parts
Term |
Definition |
attrlist |
Optional. List of attributes that apply to this class or structure. You must enclose the Lista de atributos (Visual Basic) in angle brackets (< >). |
accessmodifier |
Optional. Specifies what code can access this class or structure. See Níveis de acesso em Visual Basic. |
Shadows |
Optional. See Shadows (Visual Basic). |
MustInherit |
Optional. See MustInherit (Visual Basic). |
NotInheritable |
Optional. See NotInheritable (Visual Basic). |
name |
Required. Name of this class or structure. Must match the name defined in all other partial declarations of the same class or structure. |
Of |
Optional. Specifies that this is a generic class or structure. See Tipos genéricos no Visual Basic (Visual Basic). |
typelist |
Required if you use Of. See Lista de tipos (Visual Basic). |
Inherits |
Optional. See Declaração Inherits. |
classname |
Necessário se você usar Inherits. The name of the class or interface from which this class derives. |
Implements |
Optional. See Implementa Declaração. |
interfacenames |
Necessário se você usar Implements. The names of the interfaces this class or structure implements. |
variabledeclarations |
Optional. Statements which declare additional variables and events for the class or structure. |
proceduredeclarations |
Optional. Statements which declare and define additional procedures for the class or structure. |
End ClassouEnd Structure |
Ends this partial Class or Structure definition. |
Comentários
Visual Basic uses partial-class definitions to separate generated code from user-authored code in separate source files. For example, the Windows Form Designer defines partial classes for controls such as Form. You should not modify the generated code in these controls.
All the rules for class and structure creation, such as those for modifier usage and inheritance, apply when creating a partial class or structure.
Best Practices
Under normal circumstances, you should not split the development of a single class or structure across two or more declarations. Therefore, in most cases you do not need the Partial keyword.
Para melhor legibilidade, cadadeclaração parcialde uma classe ou estrutura deve incluir o Partialpalavra-chave. The compiler allows at most one partial declaration to omit the keyword; if two or more omit it the compiler signals an error.
Behavior
Union of Declarations. The compiler treats the class or structure as the union of all its partial declarations. Every modifier from every partial definition applies to the entire class or structure, and every member from every partial definition is available to the entire class or structure.
Type Promotion Not Allowed For Partial Types in Modules. If a partial definition is inside a module, type promotion of that class or structure is automatically defeated. In such a case, a set of partial definitions can cause unexpected results and even compiler errors. For more information, see Promoção de tipos (Visual Basic).
The compiler merges partial definitions only when their fully qualified paths are identical.
The Partial keyword can be used in these contexts:
Exemplo
The following example splits the definition of class sampleClass into two declarations, each of which defines a different Sub procedure.
Partial Public Class sampleClass
Public Sub sub1()
End Sub
End Class
Partial Public Class sampleClass
Public Sub sub2()
End Sub
End Class
The two partial definitions in the preceding example could be in the same source file or in two different source files.
Consulte também
Tarefas
Como: Criar um método parcial (Visual Basic)
Referência
Declaração Class (Visual Basic)
Conceitos
Promoção de tipos (Visual Basic)