Home »
VB.Net »
VB.Net Programs
VB.Net program to create a class to add two distances
By Nidhi Last Updated : October 13, 2024
Add two distances using class in VB.Net
Here, we will create a Distance class that contains feet and inches. Here, we will add two distances.
Program/Source Code:
The source code to create a class to add two distances is given below. The given program is compiled and executed successfully.
VB.Net code to add two distances using class
'VB.Net program to create a class to
'read and add two distance.
Class Distance
Private feet As Integer
Private inch As Integer
Sub SetDist(ByVal f As Integer, ByVal i As Integer)
feet = f
inch = i
End Sub
Sub PrintDist()
Console.WriteLine(vbTab & "Feet: " & feet)
Console.WriteLine(vbTab & "Inch: " & inch)
End Sub
Function AddDist(ByVal D As Distance) As Distance
Dim temp As New Distance()
temp.feet = feet + D.feet
temp.inch = inch + D.inch
If (temp.inch >= 12) Then
temp.feet = temp.feet + 1
temp.inch = temp.inch - 12
End If
AddDist = temp
End Function
End Class
Module Module1
Sub Main()
Dim D1 As New Distance()
Dim D2 As New Distance()
Dim D3 As New Distance()
D1.SetDist(5, 7)
D2.SetDist(6, 6)
D3 = D1.AddDist(D2)
Console.WriteLine("Distance1: ")
D1.PrintDist()
Console.WriteLine("Distance2: ")
D2.PrintDist()
Console.WriteLine("Distance3: ")
D3.PrintDist()
End Sub
End Module
Output:
Distance1:
Feet: 5
Inch: 7
Distance2:
Feet: 6
Inch: 6
Distance3:
Feet: 12
Inch: 1
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains the Main() function. Here, we created a class Distance that contains two data members feet and inch. In the Distance class we created three methods SetDist(), PrintDist(), and AddDist().
The SetDist() method is used to set the value of data members. The PrintDist() method is used to print the distance on the console screen. The AddDist() method is used to add two distances and return the addition of distance to the calling function.
The Main() function is the entry point for the program. Here, we created three objects D1, D2, and D3.
Then read of Distance from the user and then add distances using AddDist() method and then print the distances on the console screen.
VB.Net Basic Programs »