VB.Net program to create a class to add two distances

Here, we are going to learn how to create a class to add two distances in VB.Net?
Submitted by Nidhi, on December 23, 2020

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 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: ")

        Console.WriteLine("Distance2: ")

        Console.WriteLine("Distance3: ")
    End Sub
End Module


        Feet: 5
        Inch: 7
        Feet: 6
        Inch: 6
        Feet: 12
        Inch: 1
Press any key to continue . . .


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 »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.