Partial - Visual Basic (original) (raw)

Indicates that a type declaration is a partial definition of the type.

You can divide the definition of a type among several declarations by using the Partial keyword. You can use as many partial declarations as you want, in as many different source files as you want. However, all the declarations must be in the same assembly and the same namespace.

Note

Visual Basic supports partial methods, which are typically implemented in partial classes. For more information, see Partial Methods and Sub Statement.

Syntax

[ <attrlist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] _  
Partial { Class | Structure | Interface | Module } 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 type. You must enclose the Attribute List in angle brackets (< >).
accessmodifier Optional. Specifies what code can access this type. See Access levels in Visual Basic.
Shadows Optional. See Shadows.
MustInherit Optional. See MustInherit.
NotInheritable Optional. See NotInheritable.
name Required. Name of this type. Must match the name defined in all other partial declarations of the same type.
Of Optional. Specifies that this is a generic type. See Generic Types in Visual Basic.
typelist Required if you use Of. See Type List.
Inherits Optional. See Inherits Statement.
classname Required if you use Inherits. The name of the class or interface from which this class derives.
Implements Optional. See Implements Statement.
interfacenames Required if you use Implements. The names of the interfaces this type implements.
variabledeclarations Optional. Statements which declare additional variables and events for the type.
proceduredeclarations Optional. Statements which declare and define additional procedures for the type.
End Class or End Structure Ends this partial Class or Structure definition.

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, structure, interface, and module creation, such as those for modifier usage and inheritance, apply when creating a partial type.

Best Practices

Behavior

The Partial keyword can be used in these contexts:

Class Statement

Structure Statement

Example

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.

See also