Using Objects

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 3.  Basic Programming in Visual Basic .NET

Using Objects

We have talked a lot about objects. If you have programmed in C++, many of the idioms related to object-oriented programming will seem familiar to you, but in this book, we make no assumptions about your experience with object-oriented programming. Although Chapter 7 discusses defining classes in detail, including coverage of inheritance, polymorphism, and encapsulation, this section briefly introduces a few basic concepts, so you will have no trouble working with objects for now.

Visual Basic .NET supports true object-oriented programming. Critical to this is the idea of inheritance and constructors. We'll defer our discussion of inheritance until Chapter 7, but you need to know about constructors now.

What Is a Constructor?

A constructor is a member of a class that has a special roleto initialize the class. In VB6 this role was satisfied by the Class_Initialize method, which was called implicitly when you created an object.

Class_Initialize took no arguments, which meant you were unable to initialize an object with externally passed-in values. Visual Basic .NET replaces Class_Initialize with the constructor New(). New is the constructor subroutine that is responsible for initializing objects. Every object has one constructor that takes no arguments, but New can be overloaded to take one or more arguments depending on the needs of the class.

New is invoked, when you use the New keyword when creating an instance of an object, as in the following:

 Dim Button As New Button() 

This example creates an instance of a Button componentwhich replaces the Command control from VB6. Tracing into the Button class would show that the preceding statement traces right into a subroutine named New, the constructor in Visual Basic .NET. Every class can have many constructors and inherits at least one from the Object class. Listing 3.7 demonstrates a constructor call that dynamically adds a button to a form at runtime.

Listing 3.7 A Button component is dynamically constructed with New Button.
  1:  Private Sub Button1_Click(ByVal sender As System.Object, _  2:  ByVal e As System.EventArgs) Handles Button1.Click  3:   4:  Static NextTop As Integer = 0  5:  Dim Button As New Button()  6:  Button.Text = "Button" & Controls().Count  7:  Button.Top = NextTop  8:  Controls().Add(Button)  9:  NextTop = Button.Bottom  10:   11:  End Sub 

When the Click event is invoked the event handler in Listing 3.7 is called. Line 4 declares a static variable that tracks the last bottom location of the most recently created control on line 9. Line 5 constructs a new Button component. Line 6 provides a unique caption for the button, line 7 positions the button, and line 8 adds the button to the form's Controls list.

In addition to demonstrating construction, Listing 3.7 demonstrates dynamic component creation. Refer to Chapter 16, "Designing User Interfaces," for more information on dynamic component creation as a means to create flexible UIs.

Parameterized Constructors

Now that you know a constructor is the term used to refer to the New method, you might ask what a parameterized constructor is. Quite simply, a parameterized constructor is a constructor that takes arguments. Because New is essentially a method, adding arguments to New is the same as adding arguments to any method.

The slight difference in passing arguments to regular methods versus the New constructor method is that the arguments are passed in the parentheses following the data type rather than the New keyword. Here's an example:

 Dim MyException As Exception = New Exception("Raise an exception!") 

The statement constructs a new Exception object passing the string "Raise an exception!" to the constructor. From the code fragment you might assume that there is a subroutine Exception that takes a String argument, but in reality the New method is getting the argument.

Note

In C++ and Java the constructor has the same name as the class. Object Pascal, by convention, uses Create for a constructor. These languages pass the arguments to the constructor in parentheses after the constructor name . If Visual Basic .NET were to follow this convention, the construction of the Exception would look like this:

 Dim MyException As Exception = New("Raise an exception!") Exception 

That seems a little strange . Just remember that New is the constructor and param-eters are being passed to New even though it doesn't look as if this is the case.


Destructors

A destructor is a special method that is used to deinitialize an object. In VB6 the Class_Terminate method was called implicitly when an object was destroyed . Visual Basic .NET implements a garbage collector (GC) rather than explicit object destruction. The GC is a subprogram that runs in the background returning memory assigned to objects back to the memory pool.

Caution

You can explicitly run the GC, but it isn't recommended that you do so. To force the GC to run, type System.GC.Collect.

The GC is designed to run efficiently during times when your program is idle. Running the GC manually contradicts its intended use.


You cannot tell when the garbage collector will actually run and release your objects, and no equivalent of Class_Terminate is called in Visual Basic .NET.

By convention, a method named Dispose can be implemented to perform any cleanup you may need in your classes, for example, closing an open file. Chapter 7 will demonstrate how to implement and use Dispose; you may encounter examples of it in code between now and Chapter 7.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

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