Procedures


Procedures are an important tool in ASP.NET development because they allow you to determine at the time that you write the code the order in which sections of code will run at run time. Procedures also allow you to better organize your code into discrete units based on the functionality provided by each one. For example, if you write code that multiplies two numbers and returns the result, that code is much more useful if it’s wrapped in a procedure. Using a procedure allows the code to be called from more than one place in your application as necessary, which allows you to reuse the code rather than rewriting it each time you need to multiply two numbers.

In Visual Basic .NET, you’ll use two main types of procedures: Sub procedures and Function procedures. (C# has functions only.)

Sub Procedures

A Sub procedure executes code but doesn’t return a value. Code contained in a Sub procedure is delimited by the Sub and End Sub statements, as follows:

'Sub procedure that writes output to the browser Sub WriteHello() Response.Write("Hello World!") End Sub

A Visual Basic .NET Sub procedure is equivalent to a C# function declared with the void modifier.

Receiving Input Parameters

Note that in both the Visual Basic .NET Sub procedure and C# Function procedure, the name of the procedure is followed by a set of empty parentheses. These parentheses have a purpose other than simply taking up space. You can place code between these parentheses to define one or more parameters that users must pass when they call the procedure. Parameters can be passed either by value (a copy of the value is passed to the procedure) or by reference (a reference to the location of the value is passed to the procedure, which allows the procedure to modify the original value). You use the keywords ByVal or ByRef to specify which way the parameters are to be passed. If no keyword is used, the parameters will be passed by value. The following code shows some examples:

'ByVal example Sub ModifyInt(ByVal myInt As Integer) 'Add 42 to myInt myInt = myInt + 42 'The original value of myInt in the calling 'procedure, has not been modified. End Sub 'ByRef example Sub ModifyInt(ByRef myInt As Integer) 'Add 42 to myInt myInt = myInt + 42 'The original value of myInt in the calling 'procedure has now been modified. End Sub 'Implicit ByVal example Sub ModifyInt(myInt As Integer) 'Add 42 to myInt myInt = myInt + 42 'The original value of myInt in the calling 'procedure has not been modified. End Sub

Note that Visual Studio .NET will not allow the implicit use of ByVal. If you add the preceding code to a class in Visual Studio .NET, it will automatically prefix arguments with ByVal unless you explicitly prefix them with ByRef. You can pass more than one parameter to a procedure simply by separating each parameter with a comma, as in the following example:

Sub WriteHello(ByVal Name1 As String, ByVal Name2 As String) Response.Write("Hello to both " & Name1 & " and " & Name2) End Sub

Note that you explicitly declare the data type of each parameter as String. If any data type other than String is passed to the procedure, an exception will be thrown. (An exception, which you’ll learn more about later in the chapter, is similar to an error.)

Note

In Visual Basic 6, the default for a parameter definition without the ByVal or ByRef keywords is to pass the parameter by reference. Because this behavior is the opposite of that of the vast majority of languages, the Visual Basic development team has changed this behavior in Visual Basic .NET. In Visual Basic .NET, parameters defined without the ByVal or ByRef keywords are passed by value.

Whether you’re writing Visual Basic 6 code that might need to be upgraded to Visual Basic .NET or writing native Visual Basic .NET code, it’s a good programming practice to always use ByVal and ByRef to explicitly declare how your parameters should be passed.

You can make one or more of the parameters of your procedure optional (meaning the caller decides whether the parameter will be passed or not) by preceding them with the Optional keyword.

Sub WriteHello(Optional ByVal Name1 As String = "Bob") Response.Write("Hello, " & Name1 & "!") End Sub 

When you use optional parameters, you must supply a default value for each one, as in the preceding example. Also, once you’ve declared an optional parameter, all subsequent parameters for that procedure must also be optional.

Finally, you can change the accessibility of Sub procedures using the Public,Private, Friend, and Protected keywords. Sub procedures are public by default, which means they can be called from anywhere within your application.

Function Procedures

Function procedures in Visual Basic .NET are just like Sub procedures, except for one important detail—they can return a value. With a Function procedure, you could

  • Return a numeric value representing an error or success code

  • Return data in the form of an array or an ADO.NET dataset

  • Return True or False to indicate the results of some conditional test

The following code example takes two string parameters, concatenates them, and then returns the result to the caller as a string:

Function ConcatStrings(ByVal String1 As String, _ ByVal String2 As String) As String ConcatStrings = String1 & String2 End Function
Note

The preceding example uses the underscore character (_) to break a line that is too long to be displayed on a single line. This character is referred to in Visual Basic as the line continuation character, and you’ll see it in many examples throughout the book. Using the line continuation character can make your code easier to read, since you won’t need to scroll horizontally to see all of the code.

Notice that since the Function procedure itself is declared as a string, you don’t need to create a local variable to hold the result of concatenating the two strings. Instead, simply assign the result to the function name to return it to the caller.




Microsoft ASP. NET Programming with Microsoft Visual Basic. NET Version 2003 Step by Step
Microsoft ASP.NET Programming with Microsoft Visual Basic .NET Version 2003 Step By Step
ISBN: 0735619344
EAN: 2147483647
Year: 2005
Pages: 126

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