Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  OOP Basics - Shared Methods
OOP Basics - Shared Methods
by Ged Mead | Published  08/13/2010 | .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...
Introduction

Being a developer is all about making decisions and in the context of this Chess Club project, here's a decision that needs to be made.  As part of the working application, you need to know the age of club members. Perhaps the club will have different levels of membership with different fees depending on the level – Juniors, Seniors, Veterans, and so on. 

One choice you can make here is to decide is whether it would be better to use a property or a method in your class.     As the class designer, you could choose to include an Age property.  This would enable you to persist this data, but there is a potential danger.  Age, by definition changes every day (and seemingly changes faster, the older you get!).    So if you were to store a record of each instance’s age value, you'd need to build in a mechanism for checking regularly that someone hasn’t aged a year since the last time you checked, otherwise your data will be incorrect, and they'd be in the wrong category.

If you decided to create an Age property, then you have a further decision: will this be a property of the Person class, the ClubMember class or both?  

On the other hand, if you decide to create a method that calculates the age dynamically, again you need to decide the most useful and logical place to have it.    

    Your final decision will depend a great deal on your assessment of how your class will be used.   The fact is that in many cases there simply isn’t one sure-fire correct answer. 

A Shared Method

In earlier articles in this series, you've seen how to create properties and methods.  In the case of methods, this was the ToString method of the Person class.   This was an Overridden method, overriding the method in the System.Object parent class.       So you already have enough knowledge to select and implement any of those choices mentioned above.   

In this article, simply because it adds another element to your skill set, I'm going to take a slightly different route.   We'll create a new method, but will make it a Shared method.

Here’s one way of thinking about Shared methods.   Ask yourself this question:  Is it really necessary to create a whole new instance of the Person class or the ClubMember class in order to calculate an age?      

 If the answer to this question is no, then you may find that creating a Shared method to calculate an age will meet your needs perfectly.  

The key point to note about a Shared method is that it isn’t necessary to call it via an instance of the class.    In other words, if the Person class has a method named CalculateAge then it isn’t necessary to write this kind of code:

 

You don't need to create an instance of the Person class in order to access the CalculateAge method, because the method is Shared

You'll have already used Shared methods in your projects.  For example, if you've ever sorted an array with:

   Array.Sort(MyArray)

or you've deleted a file with:

   File.Delete(

"C:\Data\OldFile.txt")

then you've used a shared method.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.5 out of 5
 12 people have rated this page
Article Score2233
Sponsored Links