![]() ![]() | ||
You create objects with the New keyword, as we've seen, like this:
Dim data As New DataClass()
When you create an object, you might want to customize that object with data—for example, when you create an object of a class named Employee, you might want to store the employee's name, phone number, ID number, and so on in that object. To do that, you can use constructors, which we first discussed in Chapter 2. You pass data to a constructor by enclosing it in the parentheses following the class name when you're creating an object. The parentheses above are empty because we're not passing anything to a constructor here. (Technically, each class comes with a default constructor built in, which takes no arguments.) However, we might want to store a value, such the integer 5, in an object of a class named DataClass, like this:
Dim data As New DataClass(5)
How do you create a constructor? You add a Sub procedure named New to a class—that's all it takes. For example, here's how that might look for the DataClass class; in this case, I'm storing the value passed to New in a private data member named value, and adding a method named GetData to return that stored value as needed:
Public Class DataClass Private value As Integer Public Sub New(ByVal newValue As Integer) value = newValue End Sub Public Function GetData() As Integer Return value End Function End Class
Now I can store the value 5 inside an object of the DataClass class simply by passing 5 to the constructor, and I can retrieve that value with the GetData method:
The life cycle of an object ends when they leave scope or are set to Nothing and are released by the .NET framework. Visual Basic controls the release of system resources using procedures called destructors. In Visual Basic, objects have constructors far more often than they have destructors in general, because you typically only use destructors to clean up after an object (deallocating resources, for example, or informing other objects that the current object will no longer be available). The Finalize destructor is the one you normally use; Finalize is called automatically by the system when an object is destroyed (which means that you should not explicitly call Finalize yourself). The .NET Framework automatically runs the Finalize destructor and destroys objects when the system determines that such objects are no longer needed.
Tip |
The Sub New and Sub Finalize procedures in Visual Basic .NET replace the Class_Initializeand Class_Terminate methods used in earlier versions of Visual Basic to initialize and destroy objects. |
However, unlike the Class_Terminate procedure of VB6 and earlier, you're not supposed to be able to determine exactly when the .NET Framework will execute the Finalize method. You only can be sure that the system will call Sub Finalize some time after the last reference to an object is released. The delay between when an object leaves scope and when it is actually destroyed is because the .NET Framework uses a system called reference-tracing garbage collection that releases unused resources every now and then. Garbage collection is automatic, and it ensures that unused resources (usually memory) are always released without any extra work on your part.
To get rid of an object, then, you assign it the value Nothing. The next time garbage is collected, the object will be removed from memory.
Tip |
If you read the Visual Basic documentation, you'll read a great deal about how you can't trigger garbage collection yourself, but the fact is that you can. We'll see how to do so in this chapter in "Triggering Garbage Collection." |
![]() ![]() | ||