Section 9.6. Partial Classes


9.6. Partial Classes

Visual Basic allows a class declaration to span multiple source-code files. The separate portions of the class declaration in each file are known as partial classes. We have already created applications that use partial classesin each GUI application, the code that is auto-generated by the IDE (i.e., the code that declares and creates GUI components in the code-behind file) is marked with the Partial modifier and stored in a separate source file. For example, in the GUI application in Fig. 9.2, the lines 23 of the auto-generated partial class (stored in FrmTimeTest.Designer.vb) are:

 Partial Public Class FrmTimeTest    Inherits System.Windows.Forms.Form 


which indicates that the code in this file is part of class FrmTimeTest. The partial class is stored in a file named FrmTimeTest.Designer.vb. To view the file FrmTimeTest.Designer.vb, click the Show All Files button on the Solution Explorer toolbar and then click the plus sign (+) to the left of the FrmTimeTest.vb in the Solution Explorer window.

When a class declaration specifies the Partial modifier, any class declarations with the same class name in the program will be combined with the partial class declaration to form a single class declaration at compile time. Partial classes must be declared in the same namespace and assembly. If members (e.g., instance variables, methods and properties) in one source file conflict with those in other source files, compilation errors will occur.

Error-Prevention Tip 9.1

When combining all partial classes, at least one class declaration must have a Partial modifier. Otherwise, a compilation error occurs.


Most of the classes you have seen to this point are small. In real applications, classes can grow to be quite large and might be developed by a team of programmers. Partial classes provide a convenient way to split a large class into small, manageable pieces. With partial classes, several developers can work on the same class at once.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net