|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| The process of creating an object and separately setting some initial property values can be made easier when we define a constructor.
A constructor is always a procedure with identifier New. These constructors are implicitly called when using an object initializer ( keyword New followed by an identifier of a class ).
The constructor can define arguments, which makes it possible to state the initial property values for a new instance of this class.
Following example has a class Person with a constructor (1), which allows ( are actually requires ) to define the initial name value for each new Person. |
| Class Person
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
Private m_Name As String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
End Class
Module Client1
Sub Main()
Dim person1 As Person = New Person("John")
Console.WriteLine(person1.Name)
Console.ReadLine()
End Sub
End Module Download Broncode |
| The client is required to state an initial name value. This value is then assigned to the property Name.
To make sure the name argument value is assigned to the property Name ( and not to itself ), we refer to the property with Me.Name.
Each members of a class can be called with 'Me.MemberIdentifier' ( from within that class ), although this isn't necessary, and only required when having ambiguity between the identifier of that member and some other identifier. It can also be used to call some constructor of a class from within an other constructor of that class.
Constructors can be used to define all constructor code, all code that needs to be executed when creating an object of that type. This usually is setting some initial property values, but it can be anything you want to do when instantiating that type. |
Default Constructor and MyBase
| Every class has a constructor. Even when you don't define one, a default constructor is added in the background.
The default constructor looks like this : |
| Public Sub New()
MyBase.New()
End Sub |
| The default constructor is publicly available ( access modifier Public ). This is necessary, otherwise no objects of this class can be created outside the class.
Following example illustrates the effect of an encapsulated constructor. |
| Class SomeClass
Private Sub New()
MyBase.New()
End Sub
End Class
Module Client2
Sub Main()
End Sub
End Module Download Broncode |
| Clients ( outside 'SomeClass' ) can't create an object of type 'SomeClass'. This can be used for the "singleton pattern", further on more on this subject.
Notice that when we explicitly define a constructor, no default constructor is added.
The default constructor is parameterless.
The first line of the default constructor consists of a call to a parameterless constructor of the base class. We already know that each class we can define, is a derived class. When there is no Inherits clause explicitly added to the classdefinition, Inherits System.Object is added on the background. So the constructor of SomeClass calls a parameterless constructor of System.Object.
The MyBase keyword can be used to call a member defined in the base class. This can be an inherited member, although inherited members can be called with keyword Me preceding the identifier of this members. MyBase is only necessary when calling a constructor of a base class ( MyBase.New() ) ( constructors are not inherited ) and when a derived class is calling a members of the base class that is redefined or overshadowed in the derived class.
Me is an objectexpression, MyBase can only be used to address a member of the base class. Following construction is therefore NOT possible : |
| When a constructor is executed, the constructor will first call the constructor of its base class. By default this is a call to a parameterless constructor. When a base class doesn't have a parameterless constructor, the default call ( to the parameterless constructor ) has to be replace with an explicit call to the non-parameterless constructor. |
| Class Student : Inherits Person
Public Sub New(ByVal name As String, ByVal classGroup As String)
MyBase.New(name)
Me.ClassGroup = classGroup
End Sub
Private m_ClassGroup As String
Public Property ClassGroup() As String
Get
ClassGroup = m_ClassGroup
End Get
Set(ByVal value As String)
m_ClassGroup = value
End Set
End Property
End Class
Module Client3
Sub Main()
Dim student1 As Student = New Student("John", "Group 1")
With student1
Console.WriteLine(.Name)
Console.WriteLine(.ClassGroup)
End With
Console.ReadLine()
End Sub
End Module Download Broncode |
| Both classes ( Person and Student ) have one non-parameterless constructor. We need to place an explicit call to the constructor of Person in the constructor of Student because there is no parameterless constructor that would be called by default by the constructor of Student.
Because the constructor of 'Person' already defines what needs to happen with the provided initial 'name' value ( store it in property 'Name' ), it's a good idea anyway to reuse that behaviour by calling that constructor in the constructor of 'Student'.
A call to the constructor of a base class always need to be place on the first line of the implementation of the constructor of the derived class.
When Person would of had a parameterless constructor, no explicit calls to that constructor were needed, because the default call ( MyBase.New() ) would be sufficient. |
Up
Several Constructors in One Class
| More than one constructor can be defined in a class. Constructors can be overloaded ( method overloading ). |
| Class Teacher : Inherits Person
Public Sub New(ByVal name As String)
MyBase.New(name)
End Sub
Public Sub New(ByVal name As String, ByVal course As String)
Me.New(name)
Me.Course = course
End Sub
Private m_Course As String
Public Property Course() As String
Get
Course = m_Course
End Get
Set(ByVal value As String)
m_Course = value
End Set
End Property
End Class
Module Client4
Sub Main()
Dim teacher1 As Teacher = New Teacher("John")
Console.WriteLine(teacher1.Name)
Console.WriteLine(teacher1.Course)
Dim teacher2 As Teacher = New Teacher("Jane", "Visual Basic")
Console.WriteLine(teacher2.Name)
Console.WriteLine(teacher2.Course)
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : John
Jane
Visual Basic |
| A call like Me.New(...) is possible to call an other constructor of the same class. This call will replace the default call to the constructor of the base class. |
Up
ReadOnly Fields
| A field can be declared ReadOnly.
ReadOnly field can only be assigned a value in the constructor.
This is used for frozen states. |
| Class Product
Private m_Price As Decimal
Private ReadOnly m_TaxPercentage As Decimal
Public Sub New(ByVal taxPercentage As Decimal)
m_TaxPercentage = taxPercentage
End Sub
Public ReadOnly Property TaxPercentage() As Decimal
Get
TaxPercentage = m_TaxPercentage
End Get
End Property
Public Property Price() As Decimal
Get
Price = m_Price
End Get
Set(ByVal value As Decimal)
m_Price = value
End Set
End Property
Public Function GetPriceIncludingTax() As Decimal
GetPriceIncludingTax = Price * (1 + (TaxPercentage / 100))
End Function
End Class
Module Client5
Sub Main()
Dim product1 As Product = New Product(10)
product1.Price = 100
Console.WriteLine(product1.GetPriceIncludingTax())
Console.ReadLine()
End Sub
End Module Download Broncode |
| Tax-percentage of a product is settable at runtime ( therefore m_TaxPercentage needed to be a variable, and not a constant ), but only when creating an object of type Product ( therefore ReadOnly ). It is impossible to change the tax-percentage of a product after creation. |
Up
Exercises
| Task 1 :
What is the output of following example ? |
| Module Exercise1Task
Sub Main()
Dim object2 As Class2 = New Class2
Console.WriteLine(object2.Value1)
Console.WriteLine(object2.Value2)
Console.ReadLine()
End Sub
End Module
Class Class1
Public Value1 As Integer
Public Sub New()
Value1 = 1
End Sub
End Class
Class Class2 : Inherits Class1
Public Value2 As Integer
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise2Task
Sub Main()
Dim object4 As Class4 = New Class4
Console.WriteLine(object4.Value1)
Console.WriteLine(object4.Value2)
Console.ReadLine()
End Sub
End Module
Class Class3
Public Value1 As Integer
Public Sub New()
Value1 = 3
End Sub
End Class
Class Class4 : Inherits Class3
Public Value2 As Integer
Public Sub New()
Value2 = Value1 * 4
End Sub
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise3Task
Sub Main()
Dim object6 As Class6 = New Class6
Console.WriteLine(object6.Value1)
Console.WriteLine(object6.Value2)
object6 = New Class6(10)
Console.WriteLine(object6.Value1)
Console.WriteLine(object6.Value2)
Console.ReadLine()
End Sub
End Module
Class Class5
Public Value1 As Integer
Public Sub New()
Value1 = 2
End Sub
Public Sub New(ByVal value As Integer)
Value1 = value
End Sub
End Class
Class Class6 : Inherits Class5
Public Value2 As Integer
Public Sub New()
MyBase.New(5)
Value2 = Value1 * 6
End Sub
Public Sub New(ByVal value As Integer)
MyBase.New()
Value2 = Value1 * 6
End Sub
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise4Task
Sub Main()
Dim object8 As Class8 = New Class8
Console.WriteLine(object8.Value1)
Console.WriteLine(object8.Value2)
Console.ReadLine()
End Sub
End Module
Class Class7
Public Value1 As Integer
Public Sub New()
End Sub
Public Sub New(ByVal value As Integer)
Value1 = value
End Sub
End Class
Class Class8 : Inherits Class7
Public Value2 As Integer
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise5Task
Sub Main()
Dim object10 As Class10 = New Class10
Console.WriteLine(object10.Value1)
Console.WriteLine(object10.Value2)
Console.ReadLine()
End Sub
End Module
Class Class9
Public Value1 As Integer
Public Sub New()
End Sub
Public Sub New(ByVal value As Integer)
Value1 = value
End Sub
End Class
Class Class10 : Inherits Class9
Public Value2 As Integer
Public Sub New()
Value2 = 2
End Sub
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise6Task
Sub Main()
Dim object12 As Class12 = New Class12
Console.WriteLine(object12.Value1)
Console.WriteLine(object12.Value2)
Console.ReadLine()
End Sub
End Module
Class Class11
Public Value1 As Integer
Public Sub New()
End Sub
Public Sub New(ByVal value As Integer)
Value1 = value
End Sub
End Class
Class Class12 : Inherits Class11
Public Value2 As Integer
Public Sub New()
MyBase.New(1)
Value2 = 2
End Sub
End Class Download Broncode |
| What is the output of following example ? |
| Module Exercise7Task
Sub Main()
Dim object14 As Class14 = New Class14(1, 2)
Console.WriteLine(object14.Value1)
Console.WriteLine(object14.Value2)
Console.ReadLine()
End Sub
End Module
Class Class13
Public Value1 As Integer
Public Sub New(ByVal value As Integer)
Value1 = value
End Sub
End Class
Class Class14 : Inherits Class13
Public Value2 As Integer
Public Sub New(ByVal value1 As Integer, ByVal value2 As Integer)
MyBase.New(value1)
Me.Value2 = value2
End Sub
End Class Download Broncode |
| Write a class Counter. The value of a Counter object can be queried. Make it possible to raise ( add 1 ) and lower ( subtract 1 ) the value.
It should be mandatory to provide an initial value for the Counter when creating an object of that type. |
| Class Counter
Public Sub New(ByVal value As Integer)
m_Value = value
End Sub
Protected m_Value As Integer
Public Function GetValue() As Integer
GetValue = m_Value
End Function
Public Sub Raise()
m_Value += 1
End Sub
Public Sub Lower()
m_Value -= 1
End Sub
End Class
Module Exercise8Solution
Sub Main()
Dim counter1 As Counter = New Counter(5)
Console.WriteLine(counter1.GetValue() = 5)
Console.ReadLine()
End Sub
End Module Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|