Partial Class Definitions                                                                        
Since .NET 2.0, Visual Basic has supported the construction of ‘partial’ classes. Simply put, this allows you
implement a single class across multiple VB files. At compile time, the partial class definitions are merged into a
single class type. Consider the following Car class, defined within two VB code files.

' Car.vb
Partial Public Class Car
  ' Private member variables.

  ' Public members.
End Class



' Car.Internal.vb
Partial Public Class Car
  ' Private helper function.

  ' Other low level infrastructure.
End Class

Partial classes can help you partition a class in such as way that the ‘grunge’ can be placed into a separate
location while you can focus on the public members in the primary file. For example, Visual Studio uses partial
types for Windows Forms applications, to separate designer generated code from your code. WPF and ASP.
NET projects use partial classes in a similar manner.

Partial classes can also be helpful for team development. Multiple people can author a class at once (without
having to wait for a file to be checked into version control).

Constructors and Finalizers
To establish an object's proper in-memory image, the runtime uses a class constructor. A constructor is a special
method that is called when an object is being created. By default, the default constructor is called, which is one
that takes no arguments. If you do not explicitly define a constructor within your class, the compiler
automatically gives the class a default constructor. The compiler provided default constructor faithfully "zeros"
the memory allocated for the object's use.

Recall from the previous chapter, a subroutine called "New" is used to define a constructor. Assume the
following update to the Employee class.

Public Class Employee
...

   'A custom constructor  
   Public Sub New(ByVal name As String)
      mName = name
   End Sub      
End Class


This code has two effect. First, the compiler sees that you have provided your own constructor, which requires
a name parameter.  So, it does NOT add a default constructor to your class. Second, since the default
constructor is gone, the original object creation code we wrote is broken:

'This line no longer compiles! We are trying to call the default ctor!
Dim emp2 As Employee = New Employee()

'This line is OK, as we are specifying the 1 argument ctor.
Dim emp3 As Employee = New Employee("Homer")

If we want both of these lines of code to work, then we have to provide another version of the constructor.
Recall that is
method overloading allows methods to share names but differ in their parameter count and/or
types. Here is the Employee class, now with an overloaded constructor.

Public Class Employee
...
   'Your original constructor
   Public Sub New(ByVal Name As String)
      mName = Name
   End Sub
 
   'Overloading to provide a custom default constructor
   Public Sub New()
      mName = "Marge"
      mWage = 10
      mHours = 40
   End Sub
End Class

If the runtime calls the constructor when it creates an object, what does the runtime call when it destroys the
object? The optimal answer is "nothing". It is best to design your objects such that no special action is
necessary when the runtime destroys the object.

However, if some action were required on object destruction, then you would implement that within a method
called Finalize(). The Finalize() method is actually a ‘virtual’ method defined by the System.Object base class.
System.Object will be examined in the next chapter. Technically speaking, System.Object.Finalize() is being
‘overridden’ by your custom class, and must use the
Overrides keyword to do so. You’ll learn about virtual
methods and method overriding later in this chapter, however here is some example usage.

Public Class SomeClass

   ' You are overriding System.Object.Finalize here.
   Protected Overrides Sub Finalize()
      'Do cleanup stuff here.
   End Sub

End Class

When the .NET garbage collector destroys objects, it will call any custom Finalize() method. In a vast majority
of your classes, you will not need to build a custom Finalize() method. The only times you would need to do so
is if your VB classes are making use of unmanaged via complex COM interop or P/Invoke operations. In the rare
case that you feel you may need such support, look up the Finalize() method within the .NET SDK
documentation.
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Class Definitions, Constructors
Table of Contents
Courseware
Training Resources
Tutorials