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 compiler 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

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Comments

1

Brian - 03/08/08

Some would argue that if your class is very long, it's an indication that the class has too much responsibility and it should probably be refactored.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Brian - 08/03/08

Some would argue that if your class is very long, it's an indication that the class has too much responsibility and ... more

Log in


Sign up | Forgot your password?

What's on?