Pages

Search This Blog

Well these are all with console programming.
If you follow these programs from no.1 to no.64 you wont have any trouble learning the console part.
From next week I will start uploading windows part.
As before the programs will start from very basic to moderate level.
If you have any queries/comments/programs-you-like-to-share please do...
I will do my best to solve.
Thank you.

Simple Interface Example


Module IntefaceExample
 
    Sub Main()
        Dim obj As New Class2
        obj.add()
        obj.pro()
        Console.ReadLine()
    End Sub
 
End Module
 
Interface in1
    Sub add()
End Interface
 
Interface in2
    Sub pro()
End Interface
 
Interface in3
    Inherits in1, in2
End Interface
 
 
Public Class Class2 : Implements in3
 
    Public Sub add() Implements in1.add
        Console.WriteLine("Add Function...")
    End Sub
 
    Public Sub pro() Implements in2.pro
        Console.WriteLine("Product Function...")
    End Sub
End Class

Exception Handling Example


Module ExecptionHandlingExample
 
    Sub Main()
        Dim a, b, c As Integer
        Console.Write("Enter a and b :")
        a = CInt(Console.ReadLine)
        b = CInt(Console.ReadLine)
        Console.WriteLine("a = " + a.ToString())
        Console.WriteLine("b = " + b.ToString())
        Console.WriteLine()
        Try
            c = a \ b
            Console.WriteLine("c = " + c.ToString())
        Catch ex As Exception
            Console.WriteLine("Exception occured: " + ex.Message())
        End Try
        Console.ReadLine()
    End Sub
 
End Module


Execute Windows Programs from VB.net




Module FunProgram

    Sub Main()

        'executes/open any program which is given under quotes
        'you can run each process or can run a list of processess
        System.Diagnostics.Process.Start("notepad.exe")
        System.Diagnostics.Process.Start("www.kodderkiddie.blogspot.in")
        
    End Sub

End Module


Abstract Class Example


Public MustInherit Class Class1
    Public n As Integer
    Sub New()
        Console.WriteLine("Inside Abstract CLass1 Constructor...")
        Console.Write("Enter a number = ")
        n = CInt(Console.ReadLine)
    End Sub
    MustOverride Sub display()
End Class
 
 
Public Class Class2 : Inherits Class1
 
    Public Overrides Sub display()
        Console.WriteLine("n= " + n.ToString())
    End Sub
End Class
 
Module AbstractClassExample
    Sub Main()
        Dim obj As Class1
        Dim obj1 As New Class2
        obj = obj1
        obj.display()
        Console.ReadLine()
    End Sub
End Module


Output:
Inside Abstract CLass1 Constructor...
Enter a number = 2 
n= 2

Threading Example


Imports System.Threading

Module ThreadingDisplay

    Sub Main()
        Dim firstThread As New Thread(New ThreadStart(AddressOf Fun1))
        Dim secondThread As New Thread(New ThreadStart(AddressOf Fun2))
        firstThread.Start()
        secondThread.Start()
        firstThread.Join()
        secondThread.Join()
        Console.WriteLine("End of Main Thread...")
        Console.ReadLine()
    End Sub
    Sub fun1()

        Dim i As Integer
        For i = 1 To 10
            Console.WriteLine("From 1st Thread i= " + i.ToString())
            Thread.Sleep(1000)
        Next
    End Sub
    Sub fun2()

        Dim j As Integer
        For j = 20 To 11 Step -1
            Console.WriteLine("From 2nd Thread j= " + j.ToString())
            Thread.Sleep(1000)
        Next
    End Sub
End Module

Sum by Function Overloading


Module SumByFuncOverloading

    Sub Main()
        Dim a, b, s1, s2 As Integer
        Console.Write("Enter the value of a: ")
        a = CInt(Console.ReadLine())
        Console.Write("Enter the value of b: ")
        b = CInt(Console.ReadLine())
        Console.WriteLine()
        Console.WriteLine("Display")
        Console.WriteLine("a= " + a.ToString())
        Console.WriteLine("b= " + b.ToString())
        'function overloading
        s1 = add(a)
        s2 = add(a, b)
        Console.WriteLine()
        Console.WriteLine("Sum1= " + s1.ToString())
        Console.WriteLine("Sum2= " + s2.ToString())
        Console.ReadLine()
    End Sub
    Function add(ByVal m As Integer) As Integer
        'adding 100 to m
        m += 100
        Return m
    End Function
    Function add(ByVal p As Integer, ByVal q As Integer) As Integer
        Return p + q
    End Function
End Module

Sum Using Derived Class


Public Class Class1
    Protected a As Integer
    Sub New()
        Console.WriteLine("Inside Class1 Constructor...")
        Console.Write("Enter Number: ")
        a = CInt(Console.ReadLine())
    End Sub
    Sub display()
        Console.WriteLine("a=" + a.ToString())
    End Sub
End Class


Public Class Class2 : Inherits Class1
    Protected b As Integer
    Sub New()
        Console.WriteLine()
        Console.WriteLine("Inside Class2 Constructor...")
        Console.Write("Enter Number: ")
        b = CInt(Console.ReadLine())
    End Sub
    Sub disp()
        Console.WriteLine("b=" + b.ToString())
    End Sub
    Sub add()
        Console.Write("Sum = " + (a + b).ToString())
    End Sub
End Class


Module SumUsingDerivedClass
    Sub Main()
        Dim obj As New Class2
        Console.WriteLine()
        obj.display()
        obj.disp()
        Console.WriteLine()
        obj.add()
        Console.ReadLine()
    End Sub
End Module

Factorial using class witOUT constructor


Public Class Class2
    Function factorial(ByVal m As Integer) As Integer
        If m = 0 Then
            Return 1
        Else
            Return m * factorial(m - 1)
        End If
    End Function
End Class


Module Fact2UsClassNoConst
    Sub main()
        Dim n, fact As Integer
        Dim obj As New Class2
        Console.WriteLine("Using NO constructor")
        Console.WriteLine()
        Console.Write("Enter a Number : ")
        n = CInt(Console.ReadLine())
        If n < 0 Then
            Console.WriteLine("Factorial of negative number is NOT possible.")
        ElseIf n = 0 Or n = 1 Then
            Console.WriteLine("Factorial of " + n.ToString + " = 1")
        Else : fact = obj.factorial(n)
            Console.WriteLine("Factorial of " + n.ToString + " = " + fact.ToString())
        End If
        Console.ReadLine()
    End Sub
End Module

Factorial Using Class


Public Class Class1
    Dim n, fact As Integer
    Sub New()
        Console.Write("Enter a Number : ")
        n = CInt(Console.ReadLine())
    End Sub
    Sub checking()
        If n < 0 Then
            Console.WriteLine("Factorial of negative number is NOT possible.")
        ElseIf n = 0 Or n = 1 Then
            Console.WriteLine("Factorial of " + n.ToString + " = 1")
        Else : fact = factorial(n)
            Console.WriteLine("Factorial of " + n.ToString + " = " + fact.ToString())
        End If
    End Sub
    Function factorial(ByVal m As Integer) As Integer
        If m = 0 Then
            Return 1
        Else
            Return m * factorial(m - 1)
        End If
    End Function
End Class


Module FactUsClass
    Sub Main()
        Dim obj As New Class1
        obj.checking()
        Console.ReadLine()
    End Sub
End Module

Find Sum using Class


Public Class Class1
    Dim a, b As Integer
    Sub New()
        Console.Write("Enter 1st number : ")
        a = CInt(Console.ReadLine())
        Console.Write("Enter 2nd number : ")
        b = CInt(Console.ReadLine())
    End Sub
    Sub add()
        Dim c As Integer
        c = a + b
        Console.WriteLine()
        Console.WriteLine("Sum = " + c.ToString())
    End Sub
End Class


Module SumUsingClass
    Sub Main()
        Dim obj As New Class1
        obj.add()
        Console.ReadLine()
    End Sub
End Module

Simple Display Using Class


Public Class class1
    Dim n As Integer
    Sub New()
        Console.WriteLine("Inside Constructor...")
        Console.Write("Enter the value of n : ")
        n = CInt(Console.ReadLine())
    End Sub
    Sub display()
        Console.WriteLine("n= " + n.ToString())
    End Sub
End Class


Module DisplayUsingClass
    Sub Main()
        Dim obj As New class1
        obj.display()
        Console.ReadLine()
    End Sub
End Module

Find GCD using recursion


Module GCDRecFunc
    Sub Main()
        Dim m, n, g As Integer
        Console.Write("Enter 1st Number : ")
        m = CInt(Console.ReadLine())
        Console.Write("Enter 2nd Number : ")
        n = CInt(Console.ReadLine())
        g = gcd(m, n)
        Console.WriteLine("GCD = " + g.ToString())
        Console.ReadLine()
    End Sub
    Function gcd(ByVal x As Integer, ByVal y As Integer) As Integer
        If x Mod y = 0 Then
            Return y
        Else
            Return gcd(y, x Mod y)
        End If
    End Function
End Module

Factorial of a number using Recursion


Module FactorialRecFunc
    Sub Main()
        Dim n, fact As Integer
        Console.Write("Enter Number : ")
        n = CInt(Console.ReadLine())
        If n < 0 Then
            Console.WriteLine("Factorial of negative number is NOT possible.")
        ElseIf n = 0 Or n = 1 Then
            Console.WriteLine("Factorial of " + n.ToString() + " =1")
        Else
            fact = factorial(n)
            Console.WriteLine("Factorial of " + n.ToString() + " =" + fact.ToString())
        End If
        Console.ReadLine()
    End Sub
    Function factorial(ByVal m As Integer) As Integer
        If m = 0 Then
            Return 1
        Else
            Return m * factorial(m - 1)
        End If
    End Function
End Module

Linear Search Using Function


Module LinSearchFunc

    Sub Main()
        Console.WriteLine("Linear Searching using Function")
        Console.WriteLine()
        Dim n, i, k, item As Integer
        Console.Write("Enter Number of Elements: ")
        n = CInt(Console.ReadLine)
        Dim arr(n) As Integer
        Console.WriteLine()
        For i = 0 To n - 1
            Console.Write("Enter Element(" & (i + 1) & "): ")
            arr(i) = CInt(Console.ReadLine)
        Next
        Console.WriteLine()
        Console.WriteLine("Before Sorting")
        Console.WriteLine()
        For i = 0 To n - 1
            Console.WriteLine("Element in (" & i & "): " & arr(i))
        Next

        Console.WriteLine()
        Console.Write("Enter the Element to be searched:")
        item = CInt(Console.ReadLine())
        k = linear_search(arr, n, item)
        Console.WriteLine()
        If k <> -1 Then
            Console.WriteLine("Element is Found")
        Else
            Console.WriteLine("Element is Not Found ")
        End If
        Console.ReadLine()
    End Sub
function linear_search(byval x() as integer,byval y as integer,byval z as integer)
        Dim j As Integer
        For j = 0 To y - 1
            If x(j) = z Then
                Return j
            End If
        Next
        Return -1
    End Function
End Module

ByVal Calling


Module Module1
    Sub Main()
        Dim result As Integer
        Dim x, y As Integer
        Console.WriteLine("ByVal")
        Console.Write("Enter Original value1:")
        x = CInt(Console.ReadLine)
        Console.Write("Enter Original value2:")
        y = CInt(Console.ReadLine)
        Console.WriteLine()
        result = sum2values(x, y) 'calling the function with 2 parameters
        Console.WriteLine("Result=" & result)
        Console.WriteLine()
        Console.WriteLine("Now Values of x=" & x & " and y=" & y)
        Console.WriteLine()
        Console.WriteLine("x and y are Unchanged")
        Console.ReadLine()
    End Sub
    'defining a function with 2 parameters
    Function sum2values(ByVal x, ByVal y)
        'modifying values of x and y
        Console.Write("Enter New value1:")
        x = CInt(Console.ReadLine)
        Console.Write("Enter New value2:")
        y = CInt(Console.ReadLine)
        sum2values = x + y
    End Function

End Module

ByRef Calling


Module Module2
    Sub Main()
        Dim result As Integer
        Dim x, y As Integer
        Console.WriteLine("ByRef")
        Console.Write("Enter Original value1:")
        x = CInt(Console.ReadLine)
        Console.Write("Enter Original value2:")
        y = CInt(Console.ReadLine)
        Console.WriteLine()
        result = sum2values(x, y) 'calling the function with 2 parameters
        Console.WriteLine("Result=" & result)
        Console.WriteLine()
        Console.WriteLine("Now Values of x=" & x & " and y=" & y)
        Console.WriteLine()
        Console.WriteLine("x and y are changed")
        Console.ReadLine()
    End Sub
    'defining a function with 2 parameters
    Function sum2values(ByRef x, ByRef y)
        'modifying values of x and y
        Console.Write("Enter New value1:")
        x = CInt(Console.ReadLine)
        Console.Write("Enter New value2:")
        y = CInt(Console.ReadLine)
        sum2values = x + y
    End Function
End Module

Bubble Sorting Using Function


Module BSFun

    Sub Main()
        Console.WriteLine("Bubble Sorting using Function")
        Console.WriteLine()
        Dim n, i As Integer
        Console.Write("Enter Number of Elements: ")
        n = CInt(Console.ReadLine)
        Dim arr(n) As Integer
        Console.WriteLine()
        For i = 0 To n - 1
            Console.Write("Enter Element(" & (i + 1) & "): ")
            arr(i) = CInt(Console.ReadLine)
        Next
        Console.WriteLine()
        Console.WriteLine("Before Sorting")
        Console.WriteLine()
        For i = 0 To n - 1
            Console.WriteLine("Element in (" & i & "): " & arr(i))
        Next
        sort_array(arr, n)
        Console.WriteLine("Sorted Array")
        Console.WriteLine()
        For i = 0 To n - 1
            Console.WriteLine("Element in (" & i & "): " & arr(i))
        Next
        Console.ReadLine()
    End Sub
    Sub sort_array(ByVal x() As Integer, ByVal y As Integer)
        Dim i, j, t As Integer
        For i = 0 To y - 1
            For j = i + 1 To y - 1
                If x(i) > x(j) Then
                    t = x(i)
                    x(i) = x(j)
                    x(j) = t
                End If
            Next
        Next
    End Sub
End Module

Summation of elements of an array Using Function



Module SumArFunc

    Sub Main()
        Dim n, i, s As Integer
        Console.Write("Enter Number of elements: ")
        n = CInt(Console.ReadLine())
        Dim arr(n) As Integer
        Console.WriteLine()
        For i = 0 To n - 1
            Console.Write("Enter Element (" + (i + 1).ToString() + ")= ")
            arr(i) = CInt(Console.ReadLine())
        Next
        Console.WriteLine()
        Console.WriteLine("Array")
        Console.WriteLine("-------------")
        For i = 0 To n - 1
            Console.WriteLine("Array (" & i & ")= " & arr(i))
        Next
        Console.WriteLine("-------------")
        s = sum(arr, n)
        Console.WriteLine("Sum of Elements= " & s.ToString())
        Console.ReadLine()

    End Sub
    Function sum(ByVal x() As Integer, ByVal y As Integer)
        Dim j, s1 As Integer
        s1 = 0
        For j = 0 To y - 1
            s1 += x(j)
        Next
        Return s1

    End Function

End Module