0% found this document useful (0 votes)
33 views20 pages

Cpprog1 Chapter 8

This document covers the fundamentals of classes and objects in VB.NET programming, including how to create classes, define member functions, and utilize constructors and destructors. It explains the syntax for creating classes and objects, as well as concepts like inheritance and constructor overloading. Additionally, it provides example programs to illustrate these concepts in practice.

Uploaded by

Dalisay Bersabal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views20 pages

Cpprog1 Chapter 8

This document covers the fundamentals of classes and objects in VB.NET programming, including how to create classes, define member functions, and utilize constructors and destructors. It explains the syntax for creating classes and objects, as well as concepts like inheritance and constructor overloading. Additionally, it provides example programs to illustrate these concepts in practice.

Uploaded by

Dalisay Bersabal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MODULE INTRO TO VB.

NET PROGRAMMING– CPPROG1

CHAPTER 8: CLASSES AND OBJECTS

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.

Lesson 1: Creating a Class


A class is a group of different data members or objects with the same properties,
processes, events of an object, and general relationships to other member functions.
Furthermore, we can say that it is like a template or architect that tells what data and function
will appear when it is included in a class object. For example, it represents the method and
variable that will work on the object of the class.
Objects are the basic run-time units of a class. Once a class is defined, we can create any number
of objects related to the class to access the defined properties and methods. For example, the
Car is the Class name, and the speed, mileage, and wheels are attributes of the Class that can be
accessed by the Object.

Creating the Class


We can create a class using the Class keyword, followed by the class name. And the body of the
class ended with the statement End Class. Following is the general syntax for creating classes and
objects in the VB.NET programming language.
1. [ Access_Specifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] Class ClassNa
me
2. ' Data Members or Variable Declaration
3. ' Methods Name
4. ' Statement to be executed
5. End Class

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

o Shadows: It is an optional parameter. It represents the re-declaration of variables and


hides an identical element name or set of overloaded elements in a base class.
o MustInherit: It is an optional parameter that specifies that the class can only be used as
a base class, and the object will not directly access the base class or the abstract class.
o NotInheritable: It is also an optional parameter that representing the class not being used
as a base class.
o Partial: As the name defines, a Partial represents the partial definition of the class
(optional).
o Implements: It is used to specify interfaces from which the class inherits (optional).
My_program.vb

1. Public Class My_program


2. ' properties, method name, etc
3. ' Statement to be executed
4. End Class
In the above syntax, we have created a class with the name 'My_program' using the Class
keyword.

The Syntax for creating an object


1. Dim Obj_Name As Class_Name = New Class_Name() ' Declaration of object
2. Obj_Name.Method_Name() ' Access a method using the object
In the above syntax, we have created an instance (Obj_Name) for the class Class_Name. By using
the object name 'Obj_Name' to access all the data members and the method name
of Class_Name.
Let's create a program to find the Area and Parameter of a rectangle using the class and object
in VB.NET.
My_program.vb
1. Imports System
2. Module My_program
3. Sub Main()
4. Dim rect As Rectangle = New Rectangle() 'create an object
5. Dim rect2 As Rectangle = New Rectangle() 'create an object
6. Dim area, para As Integer
7.

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

Lesson 2: Member Function


The member function of a class is used to define the structure of member inside the
definition of the class. It can be accessed by all defined objects of the class and operated on the
data member. Furthermore, member variables are the attributes of an object to be implemented
to a member function. And we can access member variables using the public member function.

Constructor and Destructor


In VB.NET, the constructor is a special method that is implemented when an object of a particular
class is created. Constructor is also useful for creating and setting default values for a new object
of a data member.

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.

VB.NET Default Constructor Syntax


The following is the syntax for creating a constructor using the New keyword in VB.NET.
1. Public Class MyClass
2. ' Creates a Constructor using the New
3. Public Sub New()

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()

4. ' Statement or code to be executed


5. End Sub
6. End Class
Let's create a program to use the Finalize() method in VB.NET Destructor.
Destruct.vb

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>

2. ' Implementation of base class


3. End Class
4. <access_modifier> Class <Name_of_derivedClass>
5. Inherits Name_of_baseClass
6. ' Implementation of derived class

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

8. Public Overridable Sub perform()


9. Console.WriteLine(" Both Vehicles are good, but in a narrow lane, two-
wheeler are more comfortable than 4-Wheeler")
10. End Sub
11. End Class
12.
13. Class Bike
14. Inherits Vehicle
15. Public Overrides Sub perform()
16. Console.WriteLine(" Two-wheeler are lesser weight as compared to 4 wheeler")
17. End Sub
18. End Class
19. Class Car
20. Inherits Vehicle
21. Public Overrides Sub perform()
22. Console.WriteLine(" It is 4 wheelar car")
23. End Sub
24. End Class
25.
26. Sub Main()
27. Dim vehicle As Vehicle = New Vehicle()
28. Dim bike As Bike = New Bike()
29. Dim car As Car = New Car()
30. vehicle = bike
31. vehicle.perform()
32. vehicle = car
33. vehicle.perform()
34. Console.WriteLine(" Press any ley to exit...")
35. Console.ReadKey()
36. End Sub
37. End Module
Output:

Let's create a program of inheritance using the MyBase in VB.NET.

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

40. End Sub


41. End Class
42. End Module
Output:

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

3. Private Sub FetchDetails() Implements IClass.FetchDetails


4. ' Method Implementation
5. End Sub
6. End Class
In the above snippets, we inherited an interface (IClass) in the Class MyClass that implemented
to the interface method defined in a class.

Let's create and implement an instance using a class in VB.NET.


Get_Interface.vb
1. Module Get_Interface
2. Interface IStudent
3. Sub Details(ByVal y As String)
4. End Interface
5. Class Student
6. Implements IStudent
7. Public Sub Details(ByVal a As String) Implements IStudent.Details
8. ' Throw New NotImplementedException()
9. Console.WriteLine(" Name of Student: {0}", a)
10. End Sub
11. End Class
12.
13. Class Student1
14. Implements IStudent
15. Public Sub Details(ByVal a As String) Implements IStudent.Details
16. 'Throw New NotImplementedException()
17. Console.WriteLine(" Course: {0}", a)
18. End Sub
19. End Class
20. Sub Main(ByVal args As String())
21. Dim stud As IStudent = New Student()
22. stud.Details(" James Watt")

Page | 14
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

23. Dim stud1 As IStudent = New Student1()


24. stud1.Details("Computer Science")
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. End Module
Output:

For more knowledge about inheritance, please check the link provided;
https://www.youtube.com/watch?v=DW-
wSL2ms0I&list=PLsJBMeqEdtggJi2khGAjgnQ3ssgzWw_uz&index=17

Lesson 3: VB.NET Multithreading


What is VB.NET Thread?
When two or more processes execute simultaneously in a program, the process is known
as multithreading. And the execution of each process is known as the thread. A single thread is
used to execute a single logic or task in an application. By default, each application has one or
more threads to execute each process, and that thread is known as the main thread.
To create and access a new thread in the Thread class, we need to import
the System.Threading namespace. When the execution of a program begins in VB.NET, the Main
thread is automatically called to handle the program logic. And if we create another thread to
execute the process in Thread class, the new thread will become the child thread for
the main thread.
Create a new Thread

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

2. Dim th As Thread = New Thread( New ThreadStart(PrintInfo)


3. ' Start the execution of newly thread
4. th.Start()

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.

VB.NET Thread Methods


The following are the most commonly used methods of Thread class.

Page | 16
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Method Description

Abort() As the name defines, it is used to terminate the execution of a


thread.

AllocateDataSlot() It is used to create a slot for unnamed data on all threads.

AllocateNamedDatsSlot() It is used to create a slot for defined data on all threads.

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.

Join() It is a synchronization method that stops the calling thread until


the execution thread completes.

Resume() As the name suggests, a Resume() method is used to resume a


thread that has been suspended.

Sleep() It is used to suspend the currently executing thread for a specified


time.

Start() It is used to start the execution of thread or change the state of


an ongoing instance.

Suspend() It is used to stop the currently executing thread.


VB.NET Thread Life Cycle
In VB.NET Multithreading, each thread has a life cycle that starts when a new object is created
using the Thread Class. Once the task defined by the thread class is complete, the life cycle of a
thread will get the end.
There are some states of thread cycle in VB.NET programming.

State Description

Unstarted When we create a new thread, it is initially in an unstarted state.

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

Running A Running state represents that the current thread is running.

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

You might also like