Software Development

Splitting a class across files in VB.NET

VB.NET's <i>Partial</i> keyword allows you to split very long classes to multiple physical source files. Learn more about using the <i>Partial</i> keyword, and see a code example.

With the complexity of today's applications, developers often deal with classes that are very long. One way to make the classes more manageable is to split them across multiple files. Partial is a new keyword in VB.NET that allows you to split the classes to multiple physical source files. You can also use Partial to allow using multiple files for a full class definition.

An ability to split a class definition over a number of source files is generally convenient for larger projects where a number of developers may need to work on a class at the same time, or when the class definition is too long and splitting it up would make it easier to handle.

You're required to use the Partial keyword in order to tell the VB complier to collect all pieces of the class before building it. If you use multiple files, you don't have to use this keyword in every part of the class -- just in one part. However, all pieces of the same class have to be defined within the same namespace.

The compiler treats the class as a union of all its partial declarations. Every modifier from every partial definition applies to the complete class.

Note: If you use a class that inherits from another class or implements an interface, it's enough to use the Inherits or Implements keyword in just one portion of the class.

An example of Partial

Class LongClass1
      'class members defined here
End Class

Partial Class LongClass1
      'additional class members defined here
End Class

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

2 comments
mattohare
mattohare

I assume this is not in 2003. Is it new to 2005, or a version yet to come?

Editor's Picks