Cpprog1 Chapter 8
Cpprog1 Chapter 8
Objectives:
a.) Discuss the classes and objects in Vb.Net programming.
b.) Discover how to create a class in VB.net.
c.) Discover the VB.NET Thread Methods.
Where,
o Access_Specifier: It defines the access levels of the class, such as Public, Private or Friend,
Protected, Protected Friend, etc. to use the method. (It is an optional parameter).
Page | 1
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Page | 2
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
8. 'rect specification
9. rect.setLength = (5)
10. rect.setBreadth= (6)
11.
12. 'rect2 specification
13. rect2.setLength = (5)
14. rect2.setBreadth = (10)
15.
16. 'Area of rectangle
17. area = rect.length * rect.Breadth
18. 'area = rect.GetArea()
19. Console.WriteLine(" Area of Rectangle is {0}", area)
20.
21. 'Parameter of rectangle
22. 'para = rect.GetParameter()
23. para = 2 (rect2.length + rect.Breadth)
24. Console.WriteLine(" Parameter of Rectangle is {0}", para)
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. Public Class Rectangle
29. Public length As Integer
30. Public Breadth As Integer
31.
32. Public Sub setLength(ByVal len As Integer)
33. length = len
34. End Sub
35.
36. Public Sub setBreadth(ByVal bre As Integer)
37. Breadth = bre
38. End Sub
39. Public Function GetArea() As Integer
40. Return length * Breadth
41. End Function
42.
43. Public Function GetParameter() As Integer
44. Return 2 * (length + Breadth)
45. End Function
46. End Class
47. End Module
Page | 3
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Output:
For more knowledge about VB.Net class, please check the link provided;
https://www.youtube.com/watch?v=qViwO7j9Sts&list=PLsJBMeqEdtggJi2khGAjgnQ3
ssgzWw_uz&index=10
When we create a class without defining a constructor, the compiler automatically creates a
default constructor. In this way, there is a constructor that is always available in every class.
Furthermore, we can create more than one constructor in a class with the use of New keyword
but with the different parameters, and it does not return anything.
Default Constructor: In VB.NET, when we create a constructor without defining an argument, it
is called a default constructor.
4. 'Statement to be executed
Page | 4
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
5. End Sub
6. End Class
Let's create a program to define the default constructor in a VB.NET programming language.
Default_Const.vb
1. Imports System
2. Module Default_Const
3. Class Tutor
4. Public name As String
5. Public topic As String
6. ' Create a default constructor
7. Public Sub New()
8. name = "JavaTpoint"
9. topic = "VB.NET Tutorial"
10. End Sub
11. End Class
12. Sub Main()
13. ' The constructor will automatically call when the instance of the class is created
14. Dim tutor As Tutor = New Tutor() ' Create an object as a tutor
15. Console.WriteLine(" Your Site Name is : {0}", tutor.name)
16. Console.WriteLine(" Your Topic Name is : {0}", tutor.topic)
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Module
Output:
In the above example, we created a class 'Tutor' and defined a default constructor method
with New() keyword without passing any arguments. When the object (tutor) is created, the
default constructor is called into the class.
Parameterized Constructor
In VB.NET, when we pass one or more arguments to a constructor, the constructor is known as a
parameterized constructor. And the object of the class should be initialized with arguments when
it is created.
Page | 5
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Let's create a program to use the parameterized constructor to pass the argument in a class.
Para_Const.vb
1. Imports System
2. Module Para_Const
3. Class Tutor
4. Public name As String
5. Public topic As String
6. ' Create a parameterized constructor to pass parameter
7. Public Sub New(ByVal a As String, ByVal b As String)
8. name = a
9. topic = b
10. Console.WriteLine(" We are using a parameterized constructor to pass the param
eter")
11. End Sub
12. End Class
13. Sub Main()
14. ' The constructor will automatically call when the instance of the class is created
15. Dim tutor As Tutor = New Tutor("JavaTpoint", "VB.NET Constructor")
16. Console.WriteLine(" Your Site Name is : {0}", tutor.name)
17. Console.WriteLine(" Your Topic Name is : {0}", tutor.topic)
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module
Output:
VB.NET Destructor
In VB.NET, Destructor is a special function that is used to destroy a class object when the object
of the class goes out of scope. It can be represented as the Finalize() method and does not accept
any parameter nor return any value. Furthermore, it can be called automatically when a class
object is not needed.
VB.NET Destructor Syntax
Destructor using the Finalize() method in VB.NET.
Page | 6
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
1. Class My_Destructor
2. ' define the destructor
3. Protected Overrides Sub Finalize()
1. Imports System
2. Module Destruct
3. Class Get_Destroy
4. Public Sub New()
5. Console.WriteLine(" An Object of the class is being created")
6. End Sub
7. ' Use Finalize() method of Destructor
8. Protected Overrides Sub Finalize()
9. Console.WriteLine(" An Object of the class is being destroyed")
10. Console.WriteLine(" Press any key to exit")
11. End Sub
12. End Class
13.
14. Sub Main()
15. Get_Details() ' After invoking the Get_Details() method, garbage collector is called a
utomatically
16. GC.Collect()
17. Console.ReadKey()
18. End Sub
19. Public Sub Get_Details()
20. Dim dest As Get_Destroy = New Get_Destroy()
21. End Sub
22. End Module
Output:
Page | 7
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Constructor Overloading
In VB.NET, the overloading of constructors is a concept in which we can overload constructors by
defining more than one constructor with the same name but with different parameter lists to
perform different tasks.
Let's create a program to use the Constructor Overloading in a class.
Const_Over.vb
1. Imports System
2. Module Const_Over
3. Class Details
4. Public name As String
5. Public course As String
6. ' Define Default Constructor
7. Public Sub New()
8. name = "Prince"
9. course = "Coputer Science"
10. Console.WriteLine(" Uses of Overloading Constructor")
11. End Sub
12. ' Create a parametrized constructor
13. Public Sub New(ByVal a As String, ByVal b As String)
14. name = a
15. course = b
16. End Sub
17. End Class
18. Sub Main()
19. ' Called default constructor
20. Dim det As Details = New Details()
21. ' Called the parametrized constructor
22. Dim det1 As Details = New Details("Peter", "Knowledge of Data Mining")
23. Console.WriteLine(" Your Details are: Name : {0} and Course : {1}", det.name, det.co
urse)
24. Console.WriteLine(" Your Overloaded Details are: Name : {0} and Course :{1}", det1.
name, det1.course)
25. Console.WriteLine(" Press any key to exit...")
Page | 8
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
26. Console.ReadKey()
27. End Sub
28. End Module
Output:
Inheritance
In VB.NET, Inheritance is the process of creating a new class by inheriting properties and
functions from the old class and extending the functionality of the existing class. It also provides
a reusable and faster implementation time of the code. When we create a derived or inherited
class, inheritance allows us to inherit all the properties of the existing class. The old class is known
as the base class, and the inherited class is known as the derived class.
Inheritance Syntax
The following is the syntax of Inheritance in VB.NET.
1. <access_modifier> Class <Name_of_baseClass>
7. End Class
Let's create a program to understand the concept of Inheritance in VB.NET
Simple_Inherit.vb
1. Imports System
2. Module Simple_Inherit
3. Public Class Vehicle
4. Public Sub speed()
5. Console.WriteLine(" 4 Wheeler cars are more luxurious than 2 Wheeler")
6. End Sub
7.
Page | 9
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Page | 10
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Inherit_class.vb
1. Module Inherit_class
2. Class Circle 'base class
3. Protected radius As Integer
4. Public Const PI As Double = 3.14
5. Public Sub New(ByVal r As Integer)
6. radius = r
7. End Sub
8. Public Function GetAreaCircle() As Double
9. Return (PI * radius * radius)
10. End Function
11. Public Overridable Sub Show()
12. Console.WriteLine(" Radius of circle : {0}", radius)
13. Console.WriteLine(" Area of circle is {0}", GetAreaCircle())
14. End Sub
15. End Class
16.
17. 'Derived Class
18. Class MyDeriveClass : Inherits Circle
19. Private dimen As Double
20. Public Sub New(ByVal r As Integer)
21. MyBase.New(r)
22. End Sub
23. Public Function Getdimen() As Double
24. Dim dimen As Double
25. dimen = GetArea() * 50
26. Return dimen
27. End Function
28. Public Overrides Sub Show()
29. MyBase.Show()
30. Console.WriteLine("Total Cost: {0}", Getdimen())
31. End Sub
32. End Class
33.
34. Class Circle_Class
35. Shared Sub Main()
36. Dim c As MyDeriveClass = New MyDeriveClass(5)
37. c.Show()
38. Console.WriteLine("Press any key to exit")
39. Console.ReadKey()
Page | 11
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Multi-Level Inheritance
VB.NET only supports single inheritance that means a class can be inherited from the base class.
However, VB.NET uses hierarchical inheritance to extend one class to another class, which is
called Multi-Level Inheritance. For example, Class C extends Class B, and Class B extends Class A,
Class C will inherit the member of both class B and Class A. The process of extending one class to
another is known as multilevel inheritance.
1. Public Class A
2. ' implementation of code
3. End Class
4.
5. Public Class B
6. Inherits A
7. ' Implementation of Code
8. End Class
9.
10. Public Class C
11. Inherits A
12. ' Implementation of code
13. End Class
Let's create a program to understand the concept of Multi-Level Inheritance in VB.NET
Multi_inherit1.vb
1. Module Multi_inherit1
2. Public Class A
3. Public SName As String
4. Public Sub Display()
5. Console.WriteLine(" Name of Student : {0}", SName)
6. End Sub
7. End Class
Page | 12
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
8. Public Class B
9. Inherits A
10. Public place As String
11. Public Sub GetPlace()
12. Console.WriteLine(" Address : {0}", place)
13. End Sub
14. End Class
15.
16. Public Class C
17. Inherits B
18. Public rollno As Integer
19. Public Sub GetRollno()
20. Console.WriteLine(" Student Roll no. {0}", rollno)
21. End Sub
22. End Class
23.
24. Class Profile
25. Public Sub Main(ByVal args As String())
26. Dim c As C = New C()
27. c.SName = "Donald Trump"
28. c.place = "USA"
29. c.rollno = 102
30. c.Display()
31. c.GetPlace()
32. c.GetRollno()
33. Console.WriteLine(" Press any key to exit")
34. Console.ReadKey()
35. End Sub
36. End Class
37. End Module
Output:
Interface
In VB.NET, the interface is similar to a class for inheriting all properties, methods, and events that
a class or structure can implement. Using the interface in VB.NET, we can use multiple
Page | 13
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
inheritances of classes. It uses the Implements keyword to implement the interfaces, and the
Interface keyword is used to create the interface. All interfaces in VB.NET starts with i.
The Syntax for implementing an interface in a class.
1. Class MyClass
2. Inherits IClass
Page | 14
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
For more knowledge about inheritance, please check the link provided;
https://www.youtube.com/watch?v=DW-
wSL2ms0I&list=PLsJBMeqEdtggJi2khGAjgnQ3ssgzWw_uz&index=17
In VB.NET, we can create a thread by extending the Thread class and pass the ThreadStart
delegate as an argument to the Thread constructor. A ThreadStart() is a method executed by the
new thread. We need to call the Start() method to start the execution of the new thread because
it is initially in the unstart state. And the PrintInfo parameter contains an executable statement
that is executed when creating a new thread.
1. ' Create a new thread
Page | 15
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Let's write a program to create and access the thread in Thread class.
create_Thread.vb
1. Imports System.Threading 'Imports the System.Threading namespace.
2. Module create_Thread
3. Sub Main(ByVal args As String())
4. ' create a new thread
5. Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
6. ' start the newly created thread
7. th.Start()
8. Console.WriteLine(" It is a Main Thread")
9. End Sub
10. Private Sub PrintInfo()
11. For j As Integer = 1 To 5
12. Console.WriteLine(" value of j is {0}", j)
13. Next j
14. Console.WriteLine(" It is a child thread")
15. Console.WriteLine(" Press any key to exit...")
16. Console.ReadKey()
17. End Sub
18. End Module
Output:
In the above program, the main and child threads begin their execution simultaneously. The
execution of the main thread is stopped after completing its function, but the child thread will
continue to execute until its task is finished.
Page | 16
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Method Description
Equals() It is used to check whether the current and defined thread object
are equal.
Interrupt() It is used to interrupt a thread from the Wait, sleep, and join
thread state.
State Description
Runnable When we call a Start() method to prepare a thread for running, the runnable
situation occurs.
Page | 17
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Not It indicates that the thread is not in a runnable state, which means that the thread
Runnable in sleep() or wait() or suspend() or is blocked by the I/O operation.
Dead If the thread is in a dead state, either the thread has been completed its work or
aborted.
Let's create a program to manage a thread by using various methods of Thread Class.
Thread_cycle.vb
1. Imports System.Threading
2. Module Thread_cycle
3. Sub Main(ByVal args As String())
4. Dim s As Stopwatch = New Stopwatch()
5. s.Start()
6. Dim t As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
7. t.Start()
8. ' Halt another thread execution until the thread execution completed
9. t.Join()
10. s.[Stop]()
11. Dim t1 As TimeSpan = s.Elapsed
12. Dim ft As String = String.Format("{0}: {1} : {2}", t1.Hours, t1.Minutes, t1.Seconds)
13. Console.WriteLine(" Total Elapsed Time : {0}", ft)
14. Console.WriteLine("Completion of Thread Execution ")
15. Console.WriteLine("Press any key to exit...")
16. Console.ReadKey()
17. End Sub
18. Private Sub PrintInfo()
19. For j As Integer = 1 To 6
20. Console.WriteLine(" Halt Thread for {0} Second", 5)
21. ' It pause thread for 5 Seconds
22. Thread.Sleep(5000)
23. Console.WriteLine(" Value of i {0}", j)
24. Next
25. End Sub
26. End Module
Page | 18
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Output:
In the above example, we used a different method of the Thread class such as the Start() method
to start execution of the thread, the Join() method is used to stop the execution of the thread
until the execution of the thread was completed. The Sleep() method is used to pause the
execution of threads for 5 seconds.
Multithreading
When two or more processes are executed in a program to simultaneously perform multiple
tasks, the process is known as multithreading.
When we execute an application, the Main thread will automatically be called to execute the
programming logic synchronously, which means it executes one process after another. In this
way, the second process has to wait until the first process is completed, and it takes time. To
overcome that situation, VB.NET introduces a new concept Multithreading to execute multiple
tasks at the same time by creating multiple threads in a program.
Let's write a program of multiple threads to execute the multiple tasks at the same time in the
VB.NET application.
Multi_thread.vb
1. Imports System.Threading
2. Module Multi_thread
3. Sub Main(ByVal arg As String())
4. Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
5. Dim th2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo2))
6. th.Start()
7. th2.Start()
8. Console.ReadKey()
9. End Sub
10. Private Sub PrintInfo()
11. For j As Integer = 1 To 5
12. Console.WriteLine(" value of j is {0}", j)
Page | 19
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
13. Thread.Sleep(1000)
14. Next
15. Console.WriteLine(" Completion of First Thread")
16. End Sub
17. Private Sub PrintInfo2()
18. For k As Integer = 1 To 5
19. Console.WriteLine(" value of k is {0}", k)
20. Next
21. Console.WriteLine(" Completion of First Thread")
22. End Sub
23. End Module
Output:
In the above example, we have created two threads (th, th2) to execute
the PrintInfo and PrintInfo2 method at the same time. And when execution starts, both threads
execute simultaneously. But the first statement of the PrintInfo method is executed, and then it
waits for the next statement until the PrintInfo2 method is completed in the program.
For more knowledge about threading, please check the link provided;
https://www.youtube.com/watch?v=KK5D4Y2fnl4&list=PLC601DEA22187BBF1&inde
x=144
REFERENCES
https://www.javatpoint.com/vb-net-classes-and-object
https://www.javatpoint.com/vb-net-multithreading
Page | 20