18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

340<br />

Chapter 8 BUILDING CUSTOM CLASSES<br />

The statements of Listing 8.9 will produce the following output:<br />

They’re equal<br />

They’re equal<br />

The two variables are initially equal. No surprise. After modifying one of the obj2 variable’s properties,<br />

however, they’re still equal, because obj2 points to obj1. Every time we change obj2, obj1 also<br />

changes. That’s because we’ve made obj1 point to obj2. They both point to the same object (or<br />

instance of the class), and you can access this object through either class.<br />

Comment out the line that sets obj2 equal to obj1. Now, they’re not equal, even if you set all their<br />

fields to the same values. They don’t reference the same object, and it’s possible to set their properties<br />

differently.<br />

In the following section, we’ll add an Equals method that checks for value equality (as opposed to<br />

reference equality) by comparing the values of the properties of the two instances.<br />

Customizing Default Members<br />

As you recall, when you created the Minimal class for the first time, before adding any code, the<br />

class already exposed a few members—the default members, such as the ToString method (which<br />

returns the name of the class) and the Equals method (which compares two objects for reference<br />

equality). You can provide your custom implementation for these members; this is what we’re going<br />

to do in this section. You already know how to do this. Your custom ToString method must be<br />

implemented as a public function, and it must override the default implementation. The implementation<br />

of a custom ToString method is shown next:<br />

Public Overrides Function ToString() As String<br />

Return “The infamous Minimal class”<br />

End Function<br />

It’s that simple. The Overrides keyword tells the compiler that this implementation overwrites<br />

the default implementation of the class. Ours is a very simple method, but you can return any string<br />

you can build in the function. For example, you can incorporate the value of the BDate property in<br />

the string:<br />

Return(“MINIMAL: “ & tBDate.ToString)<br />

tBDate is a local variable in the class’s module, and you can use its value in any way you see fit in your<br />

code. The value of the local variable tBDate is the current value of the BDate property of the current<br />

instance of the class.<br />

When called through different variables, the ToString method will report different values. Let’s<br />

say you’ve created and initialized two instances of the Minimal class with the following statements:<br />

Dim obj1 As New Minimal()<br />

Obj1.Bdate = #1/1/1963#<br />

Dim obj2 As New Minimal()<br />

Obj2.Bdate = #12/31/1950#<br />

Console.WriteLine(obj1.ToString)<br />

Console.WriteLine(obj2.ToString)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!