Website Design United States, Website Design California, Website Designing United States, Website Designing California

Instance vs. Class

Up to this point we have created a few classes and I have been trying to avoid saying this word until now. Yes instance, what it means and the what difference between instance and class is. I referred to creating objects of a class up until now and that is what instance means. When you create an object you are creating an instance of a class.

Now how does that differ from Class? Well instance members are based on that current object and class members don't rely on anything and usually take in parameters for the necessary data. Class methods are not as common as instance methods and are usually some type of utility. You would access an instance method through a variable that holds an object (instance) of the class. The class methods do not need an object or even a variable and be accessed by the class name (i.e. ClassName.MethodName()). Instance methods are based on the object so they differ depending on the object where there is only one class method so the value wouldn't change unless there are parameters. This does not mean you can only have one class sub/function. This is where it may start to get confusing. The best way to show this is using some of VB.NET's base data types.

Users of VB6 may remember that it has some primitive data types (integer, double, string, etc). In VB.NET there aren't any primitive data types since all types have properties and methods (which makes them objects). So I will refer to them as base data types.

Start up Visual Studio.NET and create a new console application called InstancevsClass. Lets declare four variables of type Integer, String, Char, and Date. We will examine some of the instance and class methods that you may see a lot of. Set the Integer variable to 1 billion and then we can use the ToString instance method. This is a lot like the Format function and can take in a format string. We will use "0,000,000,000". Since this is an instance method it is based on the object so depending on what the integer is the function will return something different. You should end up with code similar to the following:

Sub Main()
' declare three Integer variables
Dim int1 As Integer
Dim int2 As Integer
Dim int3 As Integer
Console.Write("Please enter the first number: ")
int1 = CInt(Console.ReadLine()) ' get first integer value
Console.Write("Please enter the second number: ")
int2 = CInt(Console.ReadLine()) ' get second integer value
Console.Write("Please enter the third number: ")
int3 = CInt(Console.ReadLine()) ' get third integer value
' output the formatted integer values (that are now strings)
Console.WriteLine(int1.ToString("0,000,000,000"))
Console.WriteLine(int2.ToString("0,000,000,000"))
Console.WriteLine(int3.ToString("0,000,000,000"))
End Sub

While we are on the subject of Instance and Class there is also a couple of Class methods being called in the above code and you may not even notice it. Since "Console" is a class and there are no instances of Console but we are still accessing methods of it then those methods must be Class methods. Write, WriteLine, ReadLine, as well as others are all Class method of Console. They are all declared with the Shared keyword. This is where Class methods come in handy.

Now back to our sample. If you enter in three different numbers then the output is three different numbers. The ToString function is an instance method and therefore is based on the object. It uses the value (in this case the number) to determine what it will do/return. In our case it returns a formatted number as string in the format we specified. Since an Integer does not have a Format property or something similar to store the way the number is formatted it is taken as a parameter.

The Integer data type also has two Shared Properties (Class Properties); these are MinValue and MaxValue. These are Shared because the maximum and minimum value an Integer can hold is the same for any Integer. Therefore it is only needed once. Since it is declared as Shared any Integer can access it and every Integer will be accessing the same member and therefore the same value. This is another advantage to Shared methods or properties, they allow instances to share (hence the keyword of Shared) the same method or property and track values that are common or used by all instances.

Now you are probably wanting an example of how to create and use your own shared members. Well lets begin. I will use a simplified Rectangle. You can use the Rectangle class in previous examples if you have it. The class will look like this:

Public Class Rectangle

Private mdblLength As Double
Private mdblWidth As Double

#Region "Constructor(s)"
Public Sub New(ByVal dblLength As Double, ByVal dblWidth As Double)
If dblLength > 0 Then
mdblLength = dblLength
Else
mdblLength = 1
End If
If dblWidth > 0 Then
mdblWidth = dblWidth
Else
mdblLength = 1
End If
End Sub
#End Region

Public Property Length() As Double
Get
Return mdblLength
End Get
Set(ByVal Value As Double)
If Value > 0 Then
mdblLength = Value
End If
End Set
End Property

Public Property Width() As Double
Get
Return mdblWidth
End Get
Set(ByVal Value As Double)
If Value > 0 Then
mdblWidth = Value
End If
End Set
End Property

Public ReadOnly Property NumberOfSides() As Integer
Get
Return 4
End Get
End Property

Public Function Area() As Double
Return mdblLength * mdblWidth
End Function

Public Function Perimeter() As Double
Return (mdblLength * 2) + (mdblWidth * 2)
End Function
End Class

Looking this class over you may instantly see a good use for the Shared keyword. Q: How many sides does a rectangle have? A: 4. Q: Do all rectangles have four sides? A: Yes. There for NumberOfSides could be shared since all Rectangle instances would return a value of 4 and therefore there only need to be one occurrence of this property. Can you think of anything else? What about Area and Perimeter? What if you had a length and width and wanted to know either of them without creating a rectangle object?

The usefulness of Shared members will hopefully start to become apparent as well as how they work and how they differ from instance members.

We'll begin with the NumberOfSides property. This is easily made a Class member by inserting the Shared keyword in the property declaration.

Public Shared ReadOnly Property NumberOfSides() As Integer
Get
Return 4
End Get
End Property

That was easy :). The two functions, however, are going to be different. We still want the instance Area and Perimeter so we will add another Area and Perimeter. Now we are getting to the main difference between Class and Instance. Our current Area and Perimeter are instance methods and are based on the object's values. A Shared function does not have values and therefore it must receive those values via parameters. Both Area and Perimeter will need a length and width value).

Public Shared Function Area(ByVal dblLength As Double, _
ByVal dblWidth As Double) As Double
Return dblLength * dblWidth
End Function

Public Shared Function Perimeter(ByVal dblLength As Double, _
ByVal dblWidth As Double) As Double
Return (dblLength * 2) + (dblWidth * 2)
End Function

Notice that these are overloaded functions since they have the same name but different parameter list. You may also notice something else. The code for the two areas is very similar and the code for the two perimeters is very similar. Since the instance Area function can access the shared Area function why not use it and save us from repeating the calculation.

Public Function Area() As Double
Return Area(mdblLength, mdblWidth)
End Function

Public Function Perimeter() As Double
Return Perimeter(mdblLength, mdblWidth)
End Function

This, however, does not work the other way around. A Shared member cannot access an Instance member. Since an Instance member will differ depending on the object the Shared member wouldn't know which one it is accessing and therefore this action is not allowed. Because of that Shared members take in their values through parameters. If a Shared member needs to access an Instance then it must take it in as a parameter.

Here is a simple Sub Main() that shows you it works:

Public Sub Main()
Dim r As Rectangle
r = New Rectangle(5, 10)
Console.WriteLine("A Rectangle has " & Rectangle.NumberOfSides & " sides")
Console.WriteLine("r has an Area of " & r.Area())
Console.WriteLine("r has a perimeter of " & r.Perimeter())
Console.WriteLine("A 5 by 10 rectangle has an Area of " & _
Rectangle.Area(5, 10))
Console.WriteLine("A 5 by 10 rectangle has an Perimeter of " & _
Rectangle.Perimeter(5, 10))
End Sub

Congratulations, you have now finished the Instance vs. Class tutorial.


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com