Object-Oriented Differences

There are a couple of differences with regard to principles related to the object-oriented programming. Although these principles are discussed in detail in Part 2, “Object-Oriented Programming with Visual Basic 2010,” it’s convenient to have a small reference.

Inheritance

The first one is about inheritance, which is covered in Chapter 12. Classes (that is, reference types) support inheritance, whereas structures (value types) do not. Consider the following code:

image

In this example, the Person class is the base class and provides the basic properties for representing a hypothetical person. The Developer class inherits from Person (see the Inherits keyword), and this means that the Developer class will expose both the FirstName and LastName properties plus the new one named UsedProgrammingLanguage. It also redefines the behavior of the default ToString method so that it can return a more significant name for the object. In Visual Basic 2010 you can inherit only from one object at a time. This means that Developer can inherit only from Person but not also from another object. If you need multiple-level inheritance, you should architect your objects framework so that a second class can inherit from the first one, the third one from the second one, and so on. Structures do not support inheritance at all, except for the fact that they inherit by nature from System.ValueType.

Interfaces Implementation

Both classes and structures provide support for interfaces implementation. For example, you could implement the IComparable interface in both cases:

image

Inheritance and interfaces are discussed in Part 2, so don’t worry if something does not appear clear.

Constructors

When you declare a reference type, you need an instance before you can use it (with the exception of shared classes that are discussed in Chapter 7). Creating an instance is accomplished by invoking the constructor via the New keyword. When you instead declare a value type, the new variable is automatically initialized to a default value that is usually zero for numbers (or False for Boolean). Because of this, value types do not require invoking a default constructor. The Visual Basic compiler still accepts declaring a value type invoking the constructor, which will also initialize the type with the default value, but in this case you cannot initialize the value. The following code snippet demonstrates this:

image

Finalizers

Finalizers are a topic related to the object’s lifetime that we discuss in detail in Chapter 8. As for constructors, it’s convenient having a small reference. We previously said that when methods using value types complete their execution, they are automatically removed from the Stack together with the data. This is managed by the CLR, and because of this ordered behavior, value types do not need to be finalized. On the contrary, reference types are allocated on the heap and have a different behavior. Deallocation from memory is handled by the Garbage Collector that needs instead finalizers on the reference types side to complete its work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.150.2