Page 1 of 1

Inheritance & Generic Workshop Lesson Build a Tuple Class through using Inheritance & Generic Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Post icon  Posted 17 July 2009 - 05:23 PM

A Lesson in Inheritance & Generics
Requires: .net 3.0 or higher.
What is inheritance?
Inheritance is when an object has the traits of some other class.

What are Generics? Generics refers to a generalized of a class. (The Of bit the code. Which I'll cover later.)

In this lesson will be creating an implementation of Tuples.

So create a new console project. (This so we can focus on the code rather than making it pretty.)
Module Module1

 Sub Main()

 End Sub


End Module



Inheritance

The Basic Tuple
The Simplest Tuple Is Tuple(Of T1). The T1 denote the type for the Tuple
Public Class Tuple(Of T1)
 ' Define a protected varible for the Tuple to store a value of Type T1
 Protected mV_1 As T1
 ' Declare a property of type T1, so we can set & read the protected variable.
 Public Property V_1() As T1
  Get
   Return mV_1
  End Get
  Set(ByVal value As T1)
   mV_1 = value
  End Set
 End Property

 ' Allow the user to create a new instance of  Tuple(Of T)
 Public Sub New()
 End Sub
 ' Allow the user to create a new instance of  Tuple(Of T) and give it an initial value.
 Public Sub New(ByVal v1 As T1)
  mV_1 = v1
 End Sub

 ' Over the .ToString Function so we can return a custom format.
 Public Overrides Function ToString() As String
  Return String.Format("( {0} )", V_1)
 End Function

End Class



Now let create the next Tuple. Tuple(Of T1,T2) without using Inheritance.

One way of doing it would be
Public Class Tuple(Of T1,T2)
 ' Define a protected varible for the Tuple to store a value of Type T1
 Protected mV_1 As T1
 Protected mV_2 As T2
 ' Declare a property of type T1, so we can set & read the protected variable.
 Public Property V_1() As T1
  Get
   Return mV_1
  End Get
  Set(ByVal value As T1)
   mV_1 = value
  End Set
 End Property
 ' Declare a property of type T2, so we can set & read the protected variable.
 Public Property V_1() As T2
  Get
   Return mV_2
  End Get
  Set(ByVal value As T2)
   mV_2 = value
  End Set
 End Property
 ' Allow the user to create a new instance of  Tuple(Of T)
 Public Sub New()
 End Sub
 ' Allow the user to create a new instance of  Tuple(Of T) and give it an initial value.
 Public Sub New(ByVal v1 As T1,ByVal v2 As T2)
  mV_1 = v1
mV_2 = v2
 End Sub

 ' Over the .ToString Function so we can return a custom format.
 Public Overrides Function ToString() As String
  Return String.Format("( {0} , {1} )", V_1,V_2)
 End Function

End Class



Inheritance

But the lesson is about Inheritance also so lets use it. Comment out the previous code for Tuple(of T1,T2).
Inheritance is achieved through the keyword Inherits
Public Class Tuple(Of T1, T2)
 Inherits Tuple(Of T1)
 Protected mV_2 As T2
 Public Property V_2() As T2
  Get
   Return mV_2
  End Get
  Set(ByVal value As T2)
   mV_2 = value
  End Set
 End Property
 Public Sub New()
 End Sub

 Public Sub New(ByVal v1 As T1, ByVal v2 As T2)
 ' See by inheriting from Tuple(Of T1), mV_1 is available to us. Cool ain't it.
  mV_1 = v1
  mV_2 = v2
 End Sub
 Public Overrides Function ToString() As String
  Return String.Format("( {0} , {1} )", V_1, V_2)
 End Function
End Class



Which is easier to write? For me it the second method. Could you imagine writing Tuples(Of T1,T2,T3,T4,T5,T6) without using in inheritance.

Let implement a couple more Tuples.

Public Class Tuple(Of T1, T2, T3)
 Inherits Tuple(Of T1, T2)
 Protected mT3 As T3
 Public Property V_3() As T3
  Get
   Return mT3
  End Get
  Set(ByVal value As T3)
   mT3 = value
  End Set
 End Property
 Public Sub New()
 End Sub
 Public Sub New(ByVal m1 As T1, ByVal m2 As T2, ByVal m3 As T3)
  V_1 = m1
  V_2 = m2
  V_3 = m3
 End Sub

 Public Overrides Function ToString() As String
  Return String.Format("( {0} , {1} , {2} )", V_1, V_2, V_3)
 End Function

End Class
Public Class Tuple(Of T1, T2, T3, T4)
 Inherits Tuple(Of T1, T2, T3)
 Protected mT4 As T4
 Public Property V_4() As T4
  Get
   Return mT4
  End Get
  Set(ByVal value As T4)
   mT4 = value
  End Set
 End Property
 Public Sub New()
 End Sub
 Public Sub New(ByVal m1 As T1, ByVal m2 As T2, ByVal m3 As T3, ByVal m4 As T4)
  V_1 = m1
  V_2 = m2
  V_3 = m3
  V_4 = m4
 End Sub

 Public Overrides Function ToString() As String
  Return String.Format("( {0} , {1} , {2} , {3}) ", V_1, V_2, V_3, V_4)
 End Function

End Class




The Generic Part

Why the use of (Of T1), (Of T1,T2) etc?

To answer that lets use are new tuple classes.

In the Sub Main()

Module Module1

 Sub Main()
  Dim te1 As New Tuple(Of String)("Hello")
  Dim te2 As New Tuple(Of String, Integer)("A", 1)
  Dim te3 As New Tuple(Of String, Double)("B", 2.53)
Console.WriteLine(te)
Console.WriteLine(te2)
Console.WriteLine(te3)
 ' Pause the Console till a key is pressed
  Console.ReadKey()
 End Sub


End Module



Notice how te2 & te3 are Tuple with 2 value, both have different types.
That capability was provided be using generics, it let you focus on the structural implementation of the class.
Generics also type-checked at design time so it hard to set/gets value of the wrong type.

Run the Program

Harder Example

Add below the Console.Red()
Dim te4 As New Tuple(Of Integer,Integer,Double)(1,2,3.14)
Dim te5 As New Tuple(Of String,String,Date,Double)("Hello World!","<Dream.In.Code>",Date.Now,987)
Dim te6 As New Tuple(Of Tuple(Of Integer,Integer,Double),Tuple(Of String,String,Date,Double))(te4,te5)

Console.WriteLine(te6)
Console.ReadKey()



We're define a Tuple, that is a tuple of two tuple (one a triple and the other quadruple!).

Run It.

You'll see the values are read correctly because of inheritance.

Summary
Inheritance let you inherit stuff for another class
Generics let you be more general.

When combined they let you make complex thing simpler.

So now a I hope you have learned a few new things and you'll won't be afraid to use inheritance & Generics in your future projects. :pirate:

This post has been edited by AdamSpeight2008: 19 July 2009 - 06:34 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1