Recipe 3.9. Splitting a Class Across Multiple Files


You have a class that is simply too much to manage reasonably in a single sourcecode file, and you would like to split it up.


Use the Partial keyword on a class to enable splitting the implementation of that class across multiple physical source files:

   Partial Class Class1 End Class 


Visual Basic now includes a partial class feature that Visual Studio uses to separate automatically generated code from nongenerated code. This feature is available to use in your own classes. Before Visual Basic 2005, if you tried to split a class by using the Class statement multiple times on the same class name, the program would not compile. But now you can break up your class into separate sections:

 Class Class1    ' ----- Some class members are defined here. End Class Partial Class Class1    ' ----- More class members are defined here. End Class 

The key is the word Partial. Adding the keyword Partial to at least one of the class components tells the Visual Basic compiler to collect all the parts and put them together before it builds the compiled version of your program, even if those parts exist in different files.

You do not need to include Partial on every part of the class, just on one of the parts. Also, if your class inherits from another class or implements an interface, you need to include only the Inherits or Implements keyword in one of the class portions.

All class parts must exist in the context of the same namespace. If you create different class definitions with the same name but in different namespaces, they will be distinct and unrelated classes.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: