Changes

Jump to: navigation, search

Visual Basic Modules and Procedures

1,528 bytes added, 18:04, 31 July 2007
Defining Visual Basic Subroutines
<pre>
Public Private Sub DisplayResultButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.ShowDisplayResult("Test Message")
End Sub
Once the above change has been made, press '''F5'' to build and run the application. When the application is running, pressing the button should cause a message window to appear displaying the text "Test Message".
 
== Passing Parameters to Functions and Subroutines ==
 
Parameters may be passed to both Functions and Subroutines in Visual Basic. Parameters may be passed by ''value'' or by ''reference''. When a parameter is passed by value, the Function or Subroutine receives a ''copy'' of the data. As such, changes to the data do not affect the original object or variable. When parameters are passed by reference, however, the Function or Subroutine receives a pointer to the original variable or object. As such, any changes made in the Subroutine are made directly to the original variable or object. Parameters that are passed by ''value'' must be prefixed with the keyword ''ByValue'' in the Function or Subroutine declaration. Parameters passed by ''reference'', however, are prefixed by the ''ByRef'' keyword. If no keyword is specified, ''ByVal'' is is assumed.
 
The parameter must also be named in the Subroutine or Function declaration together with the variable or object type. For example, to adapt the ''DisplayResult()'' Subroutine to accept a ''String'' parameter called ''resultStr'' by value, and display the string in the message window:
 
<pre>
Public Sub DisplayResult(ByVal resultStr As String)
 
MessageBox.Show("Result is " + resultStr)
 
End Sub
</pre>
 
Alternatively, to pass the sting by reference:
 
<pre>
Public Sub DisplayResult(ByRef resultStr As String)
 
MessageBox.Show("Result is " + resultStr)
 
End Sub
</pre>
== Defining Visual Basic Functions ==

Navigation menu