Article Options
Premium Sponsor
Premium Sponsor

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

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.


View all articles by Ged Mead...

In the two previous articles we covered the basics of Fields, Properties and Constructors. This article looks at another Class Member - Method.

Of course, you have used methods all the time already in your coding. Subs, Functions, Procedures - you can't get very far without them. Actually, the previous articles covered some methods, because property procedures and constructors are both examples of methods which carry out specialised tasks.

Some people like to think of class members in terms of verbs and nouns - verbs being the "doing" elements and nouns being the "something which exists" elements. Fields and properties in general would be nouns. Constructors and other class methods would be verbs.

The particular method that we are going to look at in this article is the ToString method. Along the way we will take a first look at inheritance and will enlist the help of the Object Browser.

The ToString Method
Just about all .NET Types have a built in ToString method. I'm sure you will have done this or seen it lots of times in the past. Code such as:

This snippet converts an Integer type into its String representation.

In the past, you might not have given much thought to how the ToString method works. And of course you didn't need to. The designer of the Integer Class has devised a ToString method for you to use; you can write client code that takes advantage of that facility and you don't need to know how it works - just as long as it does work!

As the designers of the Person Class though, we should decide what we want to do about offering a useful ToString method for the users of our class.

You may have noticed that I used the word "useful" in that last sentence. A ToString method does already exist for the Person Class, even though it isn't actually visible in the Class code we have written so far.

When we begin to look at inheritance later, you will discover how that comes about. For the time being though, accept that it does exist but that it won't return a result that is particularly useful unless you do something to improve it.

To see the current situation, try this: Open the Class Basics project and put a Label control on this form. Put the following code in the Form_Load event of Form1 in the project:

Code Copy
        ' Create a new Person Instance
        Dim RealPerson As New Person("Ged", "Mead")
        '  Display using the default ToString method
        Label1.Text = RealPerson.ToString

Here is the result of that code:


Chances are this isn't a very useful result in most circumstances. It's accurate enough, but we would usually want a result that displayed some information about one or more properties of the RealPerson instance. You don't get that by default, but as you will see, it's easy to write code that will do just that.

Analyzing Person.ToString
The default version of ToString for the Person class will return (produce) only the name of the Namespace that holds the class and the name of the class itself. The namespace in this case is ClassBasics - which is also the name of the project in Visual Basic unless you change it). The name of the class is, of course, "Person". You can see this in the Solution Explorer screenshot below.


 In order to be able to make sense of how that default ToString method works, we are going to take a first peek at the subject of inheritance.

Sponsored Links