Nothing Special   »   [go: up one dir, main page]

VBPPT 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Procedures

 Procedures are made up of series of Visual


Basic statements that, when called, are
executed.
 There are two types of procedures in Visual
Basic .NET:
1)Sub procedures
2) Functions
. Sub procedures do not return a value, while
functions do
sub procedure with a parameter
• Sub Display (ByVal str As String)
• ByVal indicates that the text string is passed
by value, which means a copy of the string is
passed. This is the default in VB .NET.
• The other possibility is ByRef, which means
that the argument will be passed by reference.
Defining a Function

[Modifiers] Function FunctionName [(ParameterList)] As


ReturnType
[Statements]
End Function
• Modifiers − specify the access level of the function;
possible values are: Public, Private, Protected, Friend,
Protected Friend and information regarding
overloading, overriding, sharing, and shadowing.
• FunctionName − indicates the name of the function
• ParameterList − specifies the list of the parameters
• ReturnType − specifies the data type of the variable
the function returns
Functions
• Functions, which return values
• Use the keyword Function instead of Sub
Module Module1

Sub Main()
Dim a As Integer = 2
System.Console.WriteLine(“Value is: “add(a, a))
End Sub 

Function adda(ByVal int1 As Integer, ByVal int2 As Integer) As Long


Return int1 + int2
End Function

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

• We cannot call a procedure with more


arguments than the procedure declaration
specifies.
• When you need an indefinite number of
arguments, we can declare a parameter array,
which allows a procedure to accept an array of
values for an argument
Module Module1
Sub Main()
DisplayMessage("First message:", "Hi")
DisplayMessage("Second message:", "Hello", "there")
Dim TextArray() As String = {"Hello", "from", "Visual", "Basic"}
DisplayMessage("Third message:", TextArray)
Resume Next
End Sub

Sub DisplayMessage(ByVal Title As String, ByVal ParamArray text() As String)


Dim i As Integer
System.Console.WriteLine(Title)
For i = 0 To UBound(text)
System.Console.WriteLine(text(i))
Next i
End Sub
End Module
Specifying Optional Procedure Arguments

• You can make arguments optional in VB .NET


procedures if you use the Optional keyword
when declaring those arguments
• if you make one argument optional, all the
following arguments must also be optional,
and you have to specify a default value for
each optional argument
Module Module1
Sub Main()
DisplayMessage()
End Sub

  Sub DisplayMessage(Optional ByVal strText As


String = "Hello from Visual Basic")
System.Console.WriteLine(strText)
End Sub
End Module
Module Module1
Sub Main()
DisplayMessage( "Vidyalaxmi")
End Sub

Sub DisplayMessage(Optional ByVal strText As


String = "Hello from Visual Basic")
System.Console.WriteLine(strText)
End Sub
End Module
Module Module1
Sub Main()
DisplayMessage( )
End Sub

Sub DisplayMessage(Optional ByVal str As String ="Hello


from ",Optional ByVal text As String ="Visual Basic")
System.Console.WriteLine(str)
System.Console.WriteLine(text)
End Sub
End Module
Module Module1
Sub Main()
DisplayMessage("Vidya" )
End Sub

Sub DisplayMessage(Optional ByVal str As String ="Hello


from ",Optional ByVal text As String ="Visual Basic")
System.Console.WriteLine(str)
System.Console.WriteLine(text)
End Sub
End Module
Module Module1
Sub Main()
DisplayMessage(,"Vidya" )
End Sub

Sub DisplayMessage(Optional ByVal str As String ="Hello


from ",Optional ByVal text As String ="Visual Basic")
System.Console.WriteLine(str)
System.Console.WriteLine(text)
End Sub
End Module
Preserving a Variable's Values between Procedure
Module Module1 Calls
Sub Main()
Dim intLoopIndex As Integer, intValue = 0
For intLoopIndex = 0 To 4
intValue = Counter()
Next intLoopIndex
System.Console.WriteLine(intValue)
End Sub 

Function Counter() As Integer


Static intCountValue As Integer
intCountValue += 1
Return intCountValue
End Function
End Module
Creating Procedure Delegates

• Sometimes, it's useful to be able to pass the


location of a procedure to other procedures
• That location is the address of the procedure
in memory,
• To work with the address of procedures, we
use delegates in VB .NET.
Module Module1
Delegate Sub SubDelegate1(ByVal strText As String) 
Sub Main()
Dim Messager As SubDelegate1
Messager = AddressOf DisplayMessage
Messager.Invoke("Hello from Visual Basic")
End Sub 

Sub DisplayMessage(ByVal strText As String)


System.Console.WriteLine(strText)
End Sub
End Module

You might also like