VBPPT 2
VBPPT 2
VBPPT 2
Sub Main()
Dim a As Integer = 2
System.Console.WriteLine(“Value is: “add(a, a))
End Sub
End Module
Module myfunctions
Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
If (num1 > num2) Then
FindMax = num1
Else
FindMax = num2
End If
End Function
Sub Main()
Dim a As Integer = 300
Dim b As Integer = 200
Dim res As Integer
res = FindMax(a, b)
Console.WriteLine("Max value is : {0}", res)
Console.ReadLine()
End Sub
End Module
Scope Of Variable
An element's scope is its accessibility in your code.
Block scope—available only within the code block
in which it is declared
Procedure scope—available only within the
procedure in which it is declared
Module scope—available to all code within the
module, class, or structure in which it is declared
Namespace scope—available to all code in the
namespace
Module Module1
Sub Main()
Dim value As Integer = 1
If value = 1 Then
Dim strText As String = "No worries."
System.Console.WriteLine(strText)
End If
System.Console.WriteLine(strText)
'Will not work!
End Sub
End Module
Function1 as Public to Module2
Module Module1
Sub Main()
System.Console.WriteLine(Module2.Function1())
End Sub
End Module
Module Module2
Public Function Function1() As String
Return "Hello from Visual Basic"
End Function
End Module
if I declared Function1 as private to Module2, it's inaccessible in Module1
Module Module1
Sub Main() System.Console.WriteLine(Module2.Function1())
End Sub
End Module
Module Module2
Private Function Function1() As String
Return "Hello from Visual Basic"
End Function
End Module
Module Module1
Sub Main()
System.Console.WriteLine(Module2.strData)
End Sub
End Module
Module Module2
Public strData As String = "Hello from Visual Basic“
End Module
Module Module1
Sub Main()
System.Console.WriteLine(strData)
End Sub
End Module
Module Module2
Public strData As String = "Hello from Visual Basic“
End Module
Handling Exceptions
Exceptions are just runtime errors.
There are two ways of handling errors that
occur at run time in VB .NET—
1) with structured and
2) unstructured exception handling
Syntax :
Module Module1
Sub Main()
On Error Goto Handler
⋮
Exit Sub Handler:
⋮
End Sub
End Module
Module Module1
Sub Main()
Dim int1 = 0, int2 = 1, int3 As Integer
On Error Goto Handler
int3 = int2 / int1 System.Console.WriteLine("The
answer is {0}", int3)
Handler:
System.Console.WriteLine("Divide by zero error")
‘Resume Next
End Sub
End Module
Structured Exception Handling
Visual Basic uses an enhanced version of the Try…
Catch…Finally syntax already supported by other
languages
Syntax :
Module Module1
Sub Main()
Try ⋮
Catch e As Exception
⋮
End Try
End Sub
End Module
• catches an Exception object that we are
naming e. When the code in the Try block
causes an exception.
• We can use the e.ToString method to display a
message:
Module Module1
Sub Main()
Dim int1 = 0, int2 = 1, int3 As Integer
Try
int3 = int2 / int1
System.Console.WriteLine("The answer is {0}", int3)
Catch e As Exception
System.Console.WriteLine(e.ToString)
End Try
End Sub
End Module
• e.ToString method, we can also use the
e.message field, which contains this message:
Exception of type System.OverflowException
was thrown
Passing a Variable Number of Arguments