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:
' 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.
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.