Section 10.7. Class Object


10.7. Class Object

As we have mentioned, all classes inherit directly or indirectly from the Object class (namespace System), so its seven methods are inherited by all other classes. Figure 10.20 summarizes Object's methods.

Figure 10.20. Object methods that are inherited by all classes.

Method

Description

Equals

Compares two objects for equality; returns true if they are equal and False otherwise. The method takes any Object as an argument. When objects of a class must be compared for equality, the class should override method Equals to compare the contents of the two objects. The method's implementation should meet the following requirements:

  • It should return False if the argument is Nothing.

  • It should return true if an object is compared to itself, as in object1.Equals(object1).

  • It should return TRue only if both object1.Equals(object2) and object2.Equals(object1) would return TRue.

  • For three objects, if object1.Equals(object2) returns true and object2.Equals(object3) returns TRue, then object1.Equals(object3) should also return TRue.

  • A class that overrides Equals must also override GetHashCode to ensure that equal objects have identical hashcodes. The default Equals implementation determines only whether two references refer to the same object in memory.

Finalize

This Protected method (introduced in Section 9.9) is called by the garbage collector on an object just before the garbage collector reclaims the object's memory. It is not guaranteed that the garbage collector will reclaim an object, so it cannot be guaranteed that the object's Finalize method will execute. The method must specify an empty parameter list and must not return a value. The default implementation of this method is a placeholder that does nothing.

GetHashCode

A hashtable is a data structure that relates one object, called the key, to another object, called the value. We discuss class Hashtable in Chapter 26, Collections. When initially inserting a value into a hashtable, the key's GetHashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key's hashcode is also used by the hashtable to locate the key's corresponding value.

GetType

Every object in Visual Basic knows its own type at execution time. Method GetType (used in Section 11.5) returns an object of class Type (namespace System) that contains information about the object's type, such as its class name (obtained from Type property FullName).

MemberwiseClone

This Protected method, which takes no arguments and returns an Object reference, makes a copy of the object on which it is called. The implementation of this method performs a shallow copyinstance variable values in one object are copied into another object of the same type. For reference types, only the references are copied.

ReferenceEquals

This Shared method takes two Object arguments and returns TRue if two objects are the same instance or if they are Nothing references. Otherwise, it returns False.

ToString

This method (introduced in Section 9.2) returns a String representation of an object. The default implementation of this method returns the namespace followed by a dot and the class name of the object's class.


We discuss most of Object's methods throughout this book (as indicated in the table). To learn more about these methods from the Help menu, select Help > Index..., and enter "Object class" in the search text box.



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