All Main Programs on Visual Basic.
Net
'---Program : 1 {print the initial statements}
Imports System
'Program to print the Message , this is a comment
Module Module1
Sub Main()
Console.WriteLine("Program : 1 To say Hello India")
Console.WriteLine("Hello India")
Console.ReadKey() 'Like the getch() of the C
End Sub
End Module
'---Program 2
'Class Object Program , to find the area of the rectangle
Imports System
Module Module2
Sub Main()
Console.WriteLine("Program : 2 Use oops to find the area of the rectangle")
Dim x As New Rectangle()
x.Input()
x.Process()
x.Display()
Console.ReadKey()
End Sub
End Module
Public Class Rectangle
Dim Length, Width, Result As Integer
Public Sub Input()
Length = 50
Width = 10
End Sub
Public Sub Process()
Result = Length * Width
End Sub
Public Sub Display()
Console.WriteLine("The Length is ={0}", Length)
Console.WriteLine("The Width is ={0}", Width)
Console.WriteLine("The Area is ={0}", Result)
End Sub
End Class
Imports System
Module Module3
'program : average of the 5 numbers
Sub Main()
Console.WriteLine("Program : 3 find the average of 5 numbers")
Dim x As New Avg()
x.Input()
x.Process()
x.Display()
Console.ReadKey()
End Sub
End Module
Public Class Avg
Dim a1, a2, a3, a4, a5, result As Integer
Public Sub Input()
a1 = 1
a2 = 2
a3 = 3
a4 = 4
a5 = 5
End Sub
Public Sub Process()
result = (a1 + a2 + a3 + a4 + a5) / 5
End Sub
Public Sub Display()
Console.WriteLine("The Average of the numbers is : {0}", result)
End Sub
End Class
Try Parse like java in Vb.net (Single line
exception handling)
'--Program 4
'To Acept the values from the user and display its sum by the module method
Imports System
Module Module4
Sub Main()
Console.WriteLine("Program : 4 user input, display its sum by the module method")
'<---Code Below Gives Exception--->
' Dim x1, y1, result As Integer
'Console.WriteLine("Enter the 2 values for x and y")
'x1 = Console.ReadLine()
'y1 = Console.ReadLine()
'the sum is stored in the z
'result = x1 + y1
'Console.WriteLine("The Sum of the Values ={0}", result)
'<-----Solution Code is Below------>
Dim s1input, s2input As String
Dim val1, val2, result As Integer
Console.WriteLine("Caution! Integer Values Only!")
'Take input to the Strings
s1input = Console.ReadLine()
s2input = Console.ReadLine()
'Parse them as Integer and Store in val1 and val2
If Integer.TryParse(s1input, val1) And Integer.TryParse(s2input, val2) Then
Console.WriteLine("Good , Recieved Int")
Else
Console.WriteLine("Restart Giving!")
End If
result = val1 + val2
Console.WriteLine("The Sum of the Values ={0}", result)
Console.ReadKey()
End Sub
End Module
Class object Program
'---Program 5
'To make the imput from the user and then convert it into the class object
Imports System
Module Module5
Sub Main()
Console.WriteLine("Program : 5 Do Add using Class and object")
Dim x As New DoAdd()
x.Input()
x.Process()
x.Display()
Console.ReadKey()
End Sub
End Module
Public Class DoAdd
Dim Val1, Val2, Result As Integer
Public Sub Input()
Val1 = Console.ReadLine()
Val2 = Console.ReadLine()
End Sub
Public Sub Process()
Result = Val1 + Val2
End Sub
Public Sub Display()
Console.WriteLine("The 1st value = {0}", Val1)
Console.WriteLine("The 2nd Value = {0}", Val2)
Console.WriteLine("The Sum of the Values ={0}", Result)
End Sub
End Class
User Accepted Values for Which is bigger value
'Program 6 To Show 2 values Comparision by the user Accepted Values
'By the use of the if and end if statement
Imports System
Module Module6
Sub Main()
Console.WriteLine("Program : 6 Use If and End If To compare 2 user Input")
Dim x, y As Integer
x = Console.ReadLine()
y = Console.ReadLine()
'All Blocks of If are seperately closed with end if
If x > y Then
Console.WriteLine("Value x is bigger than y")
End If
If x < y Then
Console.WriteLine("Value y is Bigger than y")
End If
If x = y Then
Console.WriteLine("Both the Values are Equal")
End If
Console.ReadKey()
End Sub
End Module
Program on if and else Block
'Program 7 To make with the help of If Else and End IF
'To find the comparision btw two no.
Imports System
Module Module7
Sub Main()
Console.WriteLine("Program : 7 use If , Else If ..End If to Compare 2 No.")
Dim x, y As Integer
x = Console.ReadLine()
y = Console.ReadLine()
If x > y Then
Console.WriteLine("Value x is bigger than y")
Else
Console.WriteLine("Value y is Bigger than x")
End If
Console.ReadKey()
End Sub
End Module
'Program8
'To Accept 4 Values and then Compare to find the bigger one
Imports System
Module Module8
Sub Main()
Console.WriteLine("Program : 8 Accept 4 Values ,Compare to find the bigger one")
Dim a1, a2, a3, a4 As Integer
a1 = Console.ReadLine()
a2 = Console.ReadLine()
a3 = Console.ReadLine()
a4 = Console.ReadLine()
If a1 > a2 And a1 > a3 And a1 > a4 Then
Console.WriteLine("The Value a1 is Bigger")
End If ' yadi a 1 sabhi se bada hua
If a2 > a1 And a2 > a3 And a2 > a4 Then
Console.WriteLine("The Value a2 is Bigger")
End If
If a3 > a1 And a3 > a2 And a3 > a4 Then
Console.WriteLine("The Value a3 is Bigger")
End If
If a4 > a1 And a4 > a2 And a4 > a3 Then
Console.WriteLine("The Value a4 is Bigger")
End If
Console.ReadKey()
End Sub
End Module
if else if ladder program
'--Program 9 To make a Electric Bill Calculator
Imports System
Module Module9
Sub Main()
Console.WriteLine("Program : 9 Use If To make a Electric Bill Calculator")
Dim amt, unit As Integer
unit = Console.ReadLine()
If unit > 1 And unit <= 150 Then
amt = unit * 6.4
ElseIf unit > 150 And unit <= 250 Then
amt = unit * 7.9
ElseIf unit > 250 And unit <= 400 Then
amt = unit * 9.1
ElseIf unit > 400 And unit <= 550 Then
amt = unit * 10.9
ElseIf unit > 550 And unit <= 700 Then
amt = unit * 12.15
Else
amt = unit * 15.5
End If
Console.WriteLine("The Amount Payable: {0}", amt)
Console.ReadKey()
End Sub
End Module
'----Program 10 WAP ,User Accepted Value , to check even or odd
Imports System
Module Module10
Sub Main()
Console.WriteLine("Program : 10 WAP ,User input to check even or odd")
Dim val As Integer = Console.ReadLine()
If val = 0 Then
Console.WriteLine("The No. is 0")
ElseIf val Mod 2 <> 0 Then
Console.WriteLine("The No. is ODD")
ElseIf val Mod 2 = 0 Then
Console.WriteLine("no is even")
Else
Console.WriteLine("Restart Program")
End If
Console.ReadKey()
End Sub
End Module
Program on the select case
'---Program 11 WAP using the Select Case , to display the name of the days by the user input
Imports System
Module Module11
Sub Main()
Console.WriteLine("Program : 11 Make Days Selecter Using the Switch Case")
Console.WriteLine("Hey , USer , Give input from the no. btw 1 to 7 for days")
Dim inpp As Integer = Console.ReadLine()
Select Case inpp
Case 1
Console.WriteLine("Monday")
Case 2
Console.WriteLine("Tuesday")
Case 3
Console.WriteLine("Wednesday")
Case 4
Console.WriteLine("Thursday")
Case 5
Console.WriteLine("Friday")
Case 6
Console.WriteLine("Saturday")
Case 7
Console.WriteLine("Sunday")
Case Else
Console.WriteLine("Invalid Input")
End Select
Console.ReadKey()
End Sub
End Module
Example of the Switch Case
'---Program 12 Vb.net to make the calculator using the select Case
Imports System
Module Module12
Sub Main()
Console.WriteLine("Program : 12 User Give Any Inst . To Add (Select Case)")
Dim inpp, st1, st2 As String
Dim Val1, Val2 As Integer
Console.WriteLine("Give a /A/Add/Addition/+ to start Addition")
inpp = Console.ReadLine()
Console.WriteLine("Give Value 1")
st1 = Console.ReadLine()
Integer.TryParse(st1, Val1)
Console.WriteLine("Give Value 2")
st2 = Console.ReadLine()
Integer.TryParse(st2, Val2)
Select Case inpp
Case "+"
Case "a"
Case "A"
Case "Add"
Case "Addition"
Console.WriteLine("The Sum is ={0}", (Val1 + Val2))
Console.ReadKey()
Case Else
Console.WriteLine("Program fail!")
End Select
Console.ReadKey()
End Sub
End Module
Section : VB Programs on Arrays
By loop display the values of array
Imports System
Module Module13
Sub Main()
Console.WriteLine("Program : 13 store and Display 10 values of the array")
Dim i, x(10) As Integer
For i = 0 To 9
Console.WriteLine("Give value{0}", i)
x(i) = Console.ReadLine()
Next i
For i = 0 To 9
Console.WriteLine(x(i))
Next i
Console.ReadKey()
End Sub
End Module
All the functions that are used in the arrays
Module Module18
' for the Array Functions
Dim StudentRoll(5) As Integer
Sub Main()
Console.WriteLine("Other functions for the Arrays")
Console.WriteLine("the Upper Bound and Lower Bound methods")
For i As Integer = 0 To 4
StudentRoll(i) = 9
Console.Write(StudentRoll(i))
Next
Console.Write("Now the Size is redimensioned to 8 ")
ReDim StudentRoll(8)
For i As Integer = 0 To 7
StudentRoll(i) = 10
Console.Write(StudentRoll(i))
Next
Console.WriteLine()
Console.WriteLine("The upper Bound is by the UBound(StudentRoll)= " & UBound(StudentRoll))
Console.WriteLine()
Console.WriteLine("The lower Bound is by the LBound(StudentRoll)= " & LBound(StudentRoll))
Console.WriteLine()
Console.WriteLine("The size is by the UBound(StudentRoll)-1= " & (UBound(StudentRoll) - 1))
Console.WriteLine(" Now Redimensioning with Preserving , size inc . to 10 ")
ReDim Preserve StudentRoll(10)
StudentRoll(8) = 22
StudentRoll(9) = 33
Console.WriteLine(" Now Printing All of them")
For i As Integer = 0 To 9
Console.WriteLine(StudentRoll(i))
Next
Console.ReadKey()
End Sub
End Module
Other Methods inside the arrays
Module Module19
Sub Main()
'Other methods for the Arrays:
Console.WriteLine("The Elements of the array is in the seq:")
Dim sampleArray() As Integer = {1, 2, 3, 4, 5}
For i As Integer = 0 To UBound(sampleArray)
Console.WriteLine("the Array is Like :" & sampleArray(i))
Next
'Now For the Results of the following methods
Console.WriteLine("Output for the Array .reverse")
Array.Reverse(sampleArray)
Console.WriteLine("Output after the reversal")
For i As Integer = 0 To UBound(sampleArray)
Console.WriteLine("the Array is Like :" & sampleArray(i))
Next
Console.WriteLine("The new elements 22,66,88,99,44 are added in a sequence")
Dim sampleArray1() As Integer = {22, 66, 88, 99, 44}
Array.Sort(sampleArray1)
Console.WriteLine("The elements in the array are like")
For i As Integer = 0 To UBound(sampleArray)
Console.WriteLine("the Array is Like :" & sampleArray1(i))
Next
Console.ReadKey()
End Sub
End Module
For Clearing the elements of the array by methods
Module Module20
Sub Main()
Console.WriteLine("To clear the array from the start to end index (user Specified)")
Dim Arr(19) As Integer
For i As Integer = 0 To 4
Arr(i) = 0
'Initialising 1st 5 places with 0
Next
For i As Integer = 5 To 14
Arr(i) = 5
'Initialising middle 10 places with 5
Next
For i As Integer = 15 To 19
Arr(i) = 7
'Initialising last 5 places with 7
Next
'Printing the entire array
For i As Integer = 0 To UBound(Arr)
Console.Write(Arr(i) & ",")
Next
Console.WriteLine("1st 5 places : 0 , middle places : 5 , end places : 7")
' to clear the end 7
Array.Clear(Arr, 15, 5)
'3rd value 5 is the length , means 5 boxes ahead the 15 index
Console.WriteLine("after clear the array the result is :")
Console.WriteLine()
For i As Integer = 0 To UBound(Arr)
Console.Write(Arr(i) & ",")
Next
Console.ReadKey()
End Sub
End Module
Control Statements in Vb like in C language
Module Module21
Sub Main()
Console.WriteLine(" Control Statements in C Like Break , continue , return")
Console.WriteLine()
Console.WriteLine("Using the Continue to skip 5")
'Continue-------
Dim i As Integer
For i = 0 To 10 Step 1
If i = 5 Then
Continue For
End If
Console.WriteLine(i)
Next
Console.WriteLine()
Console.WriteLine("Using the Exit For to get out of the loop when i =5")
'Exit For-------
Dim i1 As Integer
For i1 = 0 To 10 Step 1
If i1 = 5 Then
Exit For
End If
Console.WriteLine(i1)
Next
Console.WriteLine()
Console.WriteLine("Using the return statement")
'Continue-------
Dim i2 As Integer
For i2 = 0 To 10 Step 1
If i2 = 5 Then
Dim receiveValue As Integer = i2
Console.WriteLine("Value returned ={0}", receiveValue)
End If
Next
Console.ReadKey()
End Sub
End Module
Program for the date and time
Imports System
Module Module14
Sub Main()
Dim times As DateTime = DateTime.Now()
Console.WriteLine("'1 month day , year")
Console.WriteLine("{0:MMMM dd , yyyy}", times)
Console.WriteLine("'2 month/day/year ")
Console.WriteLine("{0:M /d/yyyy}", times)
Console.WriteLine("'3 to abbreiviete the name of the week")
Console.WriteLine("{0:ddd}", times)
Console.WriteLine("'4 to show the week name as full")
Console.WriteLine("{0:dddd}", times)
Console.WriteLine("'5 to use the 12 hr clock")
Console.WriteLine("{0:hh}", times)
Console.WriteLine("'6 to use the 24 he clock")
Console.WriteLine("{0:HH}", times)
Console.WriteLine("'7 to use the Am/Pm")
Console.WriteLine("{0:tt}", times)
Console.WriteLine("'8 to show the time in the format:")
Console.WriteLine("{0:hh:mm:ss:fff}", times)
Console.ReadKey()
End Sub
End Module
Example of the Namespace and its use:
Namespace books
Public Class Myname_Author
Public Sub PrintMyName(ByVal str1 As String)
Console.WriteLine("Hello your name is " & str1)
End Sub
End Class
End Namespace
' now see its use in the program'
Imports System
Imports VbNet_Programming_Project.books
Module Module15
Sub Main()
Dim obj1 As New Myname_Author
obj1.PrintMyName("Aayush Sahay")
Console.ReadKey()
End Sub
End Module
Delegates in Visual Basic
Type 1 on the use of Delegates in Vb
Imports System
Public Delegate Sub MyDelegate()
Module Module16
'This is for the Delegates , Program No :1
'purpose : to invoke a method using a delegate
Public Class Simple
Public Sub SayHello()
MsgBox("Hello Everyone !")
End Sub
End Class
Sub Main()
Dim s As New Simple()
Dim del As MyDelegate = AddressOf s.SayHello
del()
Console.Read()
End Sub
End Module
Type 2 : Delegates Example to use the invoke method
Imports System
Module Module17
Public Delegate Sub myDel1()
Public Delegate Sub myDel2(ByVal A As Integer)
Public Sub message()
'run when no input is passed
Console.WriteLine("Hello message run , no parameters function")
End Sub
Public Sub AddTwice(ByVal a As Integer)
'run when 1 input is passed of the integer type
Dim Result As Integer = a + a
Console.WriteLine("The sum is = {0}", Result)
End Sub
' Code that would run the delegates :
Sub Main()
Dim d1 As myDel1
Dim d2 As myDel2
d1 = New myDel1(AddressOf message)
d2 = New myDel2(AddressOf AddTwice)
Console.WriteLine("The message func.")
d1.Invoke()
Console.WriteLine("The twice add func.")
d2.Invoke(50)
Console.ReadLine()
End Sub
End Module
To run the main method of the multiple programs in
the visual basic :
Imports System
Module EntryPoint
Sub Main()
Module17.Main()
' Run Module 1
Module1.Main()
' Run Module 2
Module2.Main()
' Run Module 3
Module3.Main()
' Run Module 4
Module4.Main()
'Run Module 5
Module5.Main()
' Run Module 6
Module6.Main()
' Run Module 7
Module7.Main()
' Run Module 8
Module8.Main()
' Run Module 9
Module9.Main()
' Run Module 10
Module10.Main()
' Run Module 11
Module11.Main()
' Run Module 12
Module12.Main()
' Run Module 13
Module13.Main()
End Sub
End Module
All the Forms Related Programs
The Code of the above form is:
'Date Time Picker Value is converted to the string'
Imports System.Windows.Forms
Public Class Form1
Private adate As DateTime
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
adate = DateTimePicker1.Value
Windows.Forms.MessageBox.Show("You Selected :" & adate.ToString("dd-MM-yyyy"), "Date Saved", MessageBoxButto
End Sub
End Class
The Code of the above is :
Using the Students class to use the properties
*Note : you may need to build a class file to .dll
Then You would have to refrence it , you shall learn it
from other sources
-The Students.Class code
It has the getter and setter methods in it
Public Class Students
Private id As Integer
Private fn, ln, gender, dob, pob As String
Public Property _id() As Integer
Get
Return id
End Get
Set(value As Integer)
id = value
End Set
End Property
Public Property _FirstName() As String
Get
Return fn
End Get
Set(value As String)
fn = value
End Set
End Property
Public Property _LastName() As String
Get
Return ln
End Get
Set(value As String)
ln = value
End Set
End Property
Public Property _Gender() As String
Get
Return gender
End Get
Set(value As String)
gender = value
End Set
End Property
Public Property _DOB() As String
Get
Return dob
End Get
Set(value As String)
dob = value
End Set
End Property
Public Property _POB() As String
Get
Return pob
End Get
Set(value As String)
pob = value
End Set
End Property
End Class
'summary: this is a class file having the getter and the setter methods to runtime initialise the class vairables
How the form is using it , by using the class object way
Public Class Form2
Dim st As New StudentsClass.Students
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
st._id = "1"
st._FirstName = "Aayush"
st._LastName = "Sahay"
st._Gender = "Male"
st._DOB = "29-07-2004"
st._POB = "patna"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = st._id
TextBox2.Text = st._FirstName
TextBox3.Text = st._LastName
TextBox4.Text = st._Gender
TextBox5.Text = st._DOB
TextBox6.Text = st._POB
End Sub
End Class
The form has the below code for polymorphism,
We can say method overloading and overriding
Public Class Form3
Public Class A
Public Overridable Sub disp(ByVal a As Integer, ByVal b As Integer)
MsgBox("[Class A Method]Display Sum :" & a + b)
End Sub ' for the ovverriding we use the above keyword
Public Overloads Sub Show(ByVal C As Integer)
MsgBox("[Class A] Show : " & C)
End Sub ' to achive the overloading above keyword is used
End Class 'This is the super class
Public Class B
Inherits A ' Now we can also implement the overrding and overloading methods
'in the base class b
Public Overrides Sub disp(ByVal a As Integer, ByVal b As Integer)
MsgBox("[Class B Method]Display Product :" & a * b)
'What if to also run the parent disp method
MyBase.disp(60, 70)
End Sub
'overloading means the same name but input to the procedure can be different
Public Overloads Sub Show(ByVal i1 As Integer, ByVal i2 As Integer)
MsgBox("[Class B] Show the sum of input: " & i1 + i2)
End Sub
End Class
' From the Run the methods button we will run all the methods
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'here the show is overloaded , and disp is overriden
'Make the object of the sub class
Dim b As New B()
b.Show(10)
b.Show(10, 30)
b.disp(30, 40) 'but also run the method of the class a due to mybase keyword
'product of 30 and 40 is 1200
' now also running the super class disp function
'We see that the overriding function disp , will be 1st run by the base class
'then by the help of MyBase , the Super Class disp function will run
End Sub
End Class
The Code for the Abstract Classes in Vb Net is
Public Class Form4
'the abstract class is a
Public MustInherit Class A
'procedure in the abstract class
Public MustOverride Sub ShowVal(ByVal x1 As Integer)
'it can also contain a normal procedure
Public Sub displayVal(ByVal x2 As Integer)
MsgBox("The dispVal procedure:" & x2)
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub ShowVal(x1 As Integer)
MsgBox("The ShowVal procedure from Class B:" & x1)
End Sub
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Make the object of the class B
Dim obj As New B()
obj.ShowVal(20) ' method of A but defined in B and run by b
obj.displayVal(30) 'normal procedure that was present in the Super Class
End Sub
End Class
The above will run Interfaces in VBnet
Imports VbNet_Programming_Project
Public Class Form5
Interface I
Sub display()
Function output(ByVal User_input As String) As String
End Interface
'The class that will be deriving the interface will use
'The implements keyword
Class A
Implements I
' Defining the abstract procedure
Public Sub display() Implements I.display
MsgBox("This is abst. sub , run by Class A")
End Sub
'Defining the abstract function
Public Function output(ByVal User_input As String) As String Implements I.output
Return ("User Input: " & User_input)
End Function
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' To run the methods
Dim obj As New A() ' make the object of the sub class
obj.display()
Dim get_inputToString As Object
get_inputToString = obj.output("Hello , Aayush This side")
MsgBox("Input Recieved ! from class A " & get_inputToString)
End Sub
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'there is no code for this section here...
End Sub
End Class