Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Object Oriented Programming Basics - Methods  »  The Object Browser
Object Oriented Programming Basics - Methods
by Ged Mead | Published  08/11/2009 | .NET Newbie | Rating:
The Object Browser

You can view the parentage of the Person Class in Visual Studio by using the Object Browser. You can bring the Object Browser into view in Visual Studio by selecting View | Object Browser from the menu in the IDE. It is even easier to use the shortcut key - F2.

The Object Browser is the source of a lot of useful information and recent versions are much improved. The layout of the details of the earlier versions could be quite confusing. The left hand side of the initial display will look similar to this in Visual Studio 2008:

   

(If you can't see the Form1 and Person classes in the list in your own copy of the project, you will need to click on the small plus sign at the left hand side of 'ClassBasics'. Once expanded, the plus sign changes to a minus sign, as shown in the screenshot.)

In the screenshot above, the first line is the name of the namespace. Then we have the Form1 class and the Person class as the next two items in that list.

To ensure that what you will see in your project is the same as the screenshots I will be showing you, first click on the small down arrow as shown below:

   


This will reveal list of layout choices. For now, select the 'View Namespaces' and 'Show Public Members' items only, as shown below:

   


If you next click on the word "Person" on the fourth line of the Class listing at the left hand side of the Object Browser (or click the plus sign to the left of it), you will see an immediate change to what is shown in the right hand pane.

   

That list of class members in the right hand pane is of course the four Public Properties and three Constructors that have been created previously.

Now, in order to see the members of System.Object that the Person class inherits, click on the Object Browser Settings arrow again and this time select 'Show Inherited Members':

   


Now you will some additional items in the list of members in the right hand pane:

   

You can see the final item in that list is the ToString method of the base class. That is, the ToString method of System.Object. Left-click on 'ToString' and look at the information pane below the members list:

   

You can see that it reads:
Public Overridable Function ToString() As String
Member of System.Object

We will deal with 'Overridable' shortly, but for now note that this Public Function is a member of the System.Object class. And because Person inherits from System.Object, the Person class too has a ToString method which initially is the same as its parent's ToString method.

I am going to walk through this inheritance chain again in more detail in a moment, but before leaving the Object Browser, you should note that if you double-click any of the members of the Person class, you will immediately be taken to the code line or block of that particular member in the Person.vb file. (This doesn't apply to the members of the parent class, just Person).

System.Object.ToString
At the risk of too much repetition of the same thing, I want to run through this inheritance chain once more. I have established that the base class System.Object has a ToString method. As the following code will show, the ToString method of the System.Object parent class is configured simply to return the fully qualified name of the class . This code snippet:

Code Copy
       Dim obj As New Object
       Label1.Text = obj.ToString

will produce the following result:

   

As you can see, the System.Object.ToString method will return the Type name, which of course is "System.Object".

Now if you transfer this logic to the Person class that we created (which inherits directly from System.Object), the inherited ToString function will again return the Type name. This time though the Type name will be "ClassBasics.Person" .

   

So we've come full circle and arrived back at the unhelpful Person.ToString method. It's important that you understand how and why we get this result and I hope that the previous explanations will have clarified this for you.

Let's move on and find that way of telling the compiler that we want to replace this original version of the ToString method with a more useful method of our own. What we really want to achieve is a ToString method which will return something like:

   

Sponsored Links