VB.Net program to create a user-defined namespace with structures

Here, we are going to learn how to create a user-defined namespace with structures in VB.Net?
Submitted by Nidhi, on December 29, 2020 [Last updated : March 08, 2023]

User-defined namespace with structures in VB.Net

Here, we will create a user-defined namespace that contains two structures. Here, each structure contains methods to perform mathematical operations.

Program/Source Code:

The source code to create a user-defined namespace with structures is given below. The given program is compiled and executed successfully.

VB.Net code to create a user-defined namespace with structures

'VB.net program to create a user-defined namespace 
'with structures.

Namespace MathNamespace
    Structure AritheMatic
        Sub Add(ByVal num1 As Integer, ByVal num2 As Integer)
            Console.WriteLine("Addition is: {0}", num1 + num2)
        End Sub

        Sub Subtract(ByVal num1 As Integer, ByVal num2 As Integer)
            Console.WriteLine("Subtraction is: {0}", num1 - num2)
        End Sub
    End Structure

    Structure Fact
        Sub CalculateFactorial(ByVal num As Integer)
            Dim fact As Integer = 1

            For i = num To 1 Step -1
                fact = fact * i
            Next
            Console.WriteLine("Factorial is: {0}", fact)
        End Sub
    End Structure

End Namespace

Module Module1

    Sub Main()
        Dim airth As New MathNamespace.AritheMatic
        Dim fact As New MathNamespace.Fact

        airth.Add(10, 20)
        airth.Subtract(20, 10)

        fact.CalculateFactorial(5)
    End Sub
    
End Module

Output

Addition is: 30
Subtraction is: 10
Factorial is: 120
Press any key to continue . . .

Explanation

In the above program, we created a namespace that contains two structures Arithmetic and Fact. Both structures contain some methods to perform mathematical operations.

After that, we created a module Module1 that contains the main() method, the main() method is the entry point for the program. Here we created the object of both structures and then called methods to perform mathematical operations.

VB.Net Namespaces Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.