System Object

For now, the one key to remember is that all classes in Visual Basic, all classes in .NET for that matter, inherit from the base class System.Object. System.Object is the parent class for everything from Strings and Integers to Windows and custom classes developed by you. When a Class doesn't explicitly state its parent, .NET will automatically have the class inherit from System.Object. This also means that even if a Class does explicitly inherit from another Class, that Parent Class or its parent or base Class, or at some point some class in the inheritance chain, will inherit from System.Object. There was an old saying about how all roads lead to Rome. Well, in .NET all object hierarchies lead to System.Object. System.Object is the base Class for all Classes.

This is where the term polymorphism becomes important. Since you can cast any object to a type from which it inherits, any type of object can be cast to the base class of System.Object. Casting is the name for code which takes an object of, for example, type Integer and assigns it to a variable of type System.Object. As you'll see as you work with Visual Basic or another object oriented language, this means that you'll see many methods that are written to handle a parameter of type Object. This has several advantages for code reuse, but you'll learn that reuse can come at a cost.

The thing you'll want to keep in mind is that when an object is cast to its parent class it only makes the methods of that parent class available. While the underlying object can be cast back to its original type, because it knows what that type is, it doesn't make all of its data and behavior available until it is cast back to its original type. Note you can cast to any type in an object's inheritance hierarchy, but that will be covered in more detail in Chapter 3.

System.Object provides a member in which you will be interested. The method ToString provides a way to get a string representation of any object. The default implementation of this method will return the type of that object; however, many types provide a custom implementation of this method, and doing so is considered best practice.


Having introduced a combination of keywords and concepts for objects in Visual Basic, it is time to start exploring specific types. In order to ensure this is hands on, you need a project in Visual Studio 2010. The previous chapter focused on Visual Studio 2010 and many of its features as your primary development tool. This chapter is much more focused on the Visual Basic language, and to limit its size you are going to reference the project created in the last chapter in this chapter. As this chapter introduces some of the core types of Visual Basic, classes provided by the .NET Framework which are at the core of Visual Basic, you test code snippets in that project.

To host the example snippets in the project based on the ProVB_VS2010 project built in Chapter 1, there are very few changes. The code download for this chapter includes the final version of the samples. This means that as you progress through the chapter, you can either look in the sample download for the same code, or step by step build up your own copy of the sample, using the sample when something doesn't seem quite right in your own code. The one change from the previous chapter's code is that the custom property and message box used in that chapter have already been removed from this project.

At this point, you have a display that allows you to show the results from various code snippets simply by updating the Text property on the TextBox1 control of your window. The display for the baseline Windows Form application is shown in Figure 2-1.

figure 2-1

As you can see, the baseline is simply a button to initiate the custom code combined with a text box. As you want to test a snippet, you will add a new method to Form1, and then call this method from the Click event handler for the button. The sample download shows how these methods can be added, and then when you are ready to add the next method, use the single quote line to comment out the method call for the previous test. In this way, you'll be able to comment out a single line call to your method and within the method the code to demonstrate that feature of Visual Basic will remain available. The following section on the difference between value and reference types will create two such methods, and if you look at the sample download code you'll find the calls to the methods shown in the snippet are in the event handler commented out already.

Was this article helpful?

0 0

Post a comment