PASSING ARGUMENTS


Procedures can be set up to process data that is passed to them in the form of arguments. These arguments are passed to a procedure and mapped to parameters that are defined as part of the procedure's declaration, as demonstrated below.

 Public Sub DisplayMessage(ByVal strMessage As String)      MessageBox.Show(strMessage, "Sample Message") End Sub 

Trick 

You can set up your Sub procedures to process as many arguments as you choose to pass to it, as long as you separate each corresponding argument definition with a comma.

In this example, a Sub type procedure named DisplayMessage is defined. The procedure defines a single variable, a parameter named strMessage with a data type of String. The argument passed to the Sub procedure is automatically assigned to the strMessage variable. The argument is passed by value (ByVal), meaning that any change made to the value of strMessage will have no effect on the value assigned to the argument passed to the procedure. You can also pass an argument by reference (ByRef). However, this is not the default option. In fact, if you fail to specify ByVal or ByRef when you define your procedure, Visual Basic will automatically insert ByVal for you.

image from book
DEFINITION

An argument is a value passed to a procedure for processing.

image from book

You can pass as many arguments as required to a procedure, as long as you separate each argument by a comma when calling the procedure, as demonstrated below.

image from book
DEFINITION

A parameter is a declaration within a procedure that defines variables to be used to store copies of values passed to them as arguments.

image from book

 DisplayMessage("Click on Yes to accept.", "Sample Message", "question") 

Like Sub procedures, you can set up Function procedures to process data that is passed to the procedures in the form of arguments. These arguments are passed to a procedure and mapped to parameters that are defined as part of the procedure's declaration, as demonstrated below.

 Public Function DisplayMessage(ByVal strMessage As String,_   ByVal strTitle As String) As String      Dim intResult As Integer = 0      intResult = MessageBox.Show(strMessage, strTitle, _        MessageBoxButtons.YesNo)      If intResult = 6 Then           Return "Yes"      Else           Return "No"      End If End Function 




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net