Your full service technology partner! 
-Collapse +Expand
Search VB.Net Group:

-Collapse +Expand VB.Net To/From
-Collapse +Expand VB.Net Store

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingVB.NetOOP   Print This     
  From the October 2015 Issue of Prestwood eMag
VB.Net Partial Classes (Partial)
Posted 10 years ago on 2/3/2009
VB.Net Code Snippet:
 A flashcard from our VB.Net Flashcards Library
 A code snippet from our VB.Net Code Snippets Page


General Info: Partial Class

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.

Syntax Example:
Partial Public Class Cyborg
Inherits System.Object
End Class

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"
  End Sub
End Class
Partial Public Class Cyborg
  Inherits System.Object
  Private FCyborgName As String
  Public Property CyborgName()
      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 & ".")
      MessageBox.Show("Hi, I do not have a name yet.")
    End If
  End Sub
End Class

More Info

Definition:  Partial Class


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1261A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101857 Counter
Since 2/3/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2019 Prestwood IT Solutions.   [Security & Privacy]