A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.
Languages Focus: Partial Class
For languages that have implemented partial classes, you need to know usage details and restrictions. Can you split a class into two or more files? Can you split a class within a source code file into two or more locations? What are the details of inheritance? Does it apply to interfaces as well?
VB.Net Partial Classes
VB.Net supports both partial classes and partial methods.
Partial classes and VB.Net
Microsoft added the concept of partial classes to VB.Net 2005 (along with the arrival of the .Net Framework 2). With the .Net implementation, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.
.Net Features
- Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
- Any class, struct, or interface members declared in a partial definition are available to all of the other parts.
.Net Limitations
- All parts must be defined within the same namespace.
- All the parts must use the partial keyword.
- All of the parts must be available at compile time to form the final type.
- All the parts must have the same accessibility (i.e. public, private, etc.).
- If any of the parts are declared abstract, then the entire type is abstract.
- If any of the parts are declared sealed, then the entire type is sealed.
- If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.
VB.Net 2008 Working Winforms Example
The following simple example demonstrates partial classes. Although partial classes have many uses, in this demonstration I am splitting the properties from the methods (only one of each). In this demo, all code is in this one file along with the form. However, in a real coding situation you could put the methods and properties in their own source files as in CyborgProperties.vb and CyborgMethods.vb or divide the code in any other way you wish. This is particularly convenient with large classes in a multi-developer environment where the team is using a version control system.
Create a form and place a button on it and alter code as follows:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyRobot As Cyborg
MyRobot = New Cyborg
MyRobot.CyborgName = "Cameron"
MyRobot.IntroduceYourself()
End Sub
End Class
Partial Public Class Cyborg
Inherits System.Object
Private FCyborgName As String
Public Property CyborgName()
Get
Return FCyborgName
End Get
Set(ByVal value)
FCyborgName = value
End Set
End Property
End Class
Partial Public Class Cyborg
Public Sub IntroduceYourself()
If CyborgName.ToString.Length > 0 Then
MessageBox.Show("Hi, my name is " & CyborgName & ".")
Else
MessageBox.Show("Hi, I do not have a name yet.")
End If
End Sub
End Class