VB Programming in ASP.NET

VB Programming in ASP.NET

VB Programming in ASP.NET

As it was mentioned before, ASP.NET supports several languages such as VB.NET, C-Sharp and J-Script. In this section, we will discuss how VB.NET program structures are implemented in ASP.NET pages. Mainly, we will some examples.

Using Variables

Variables are used in all programming language to hold data during the run-time of a program. All variables are declared within the <script...</script> tags. The following is an example that uses three asp.NET label controls to display values stored in three variables. The script is run at the page load event.

You can use a text editor to copy the code and save the file with a .aspx extension in your web folder. Then use full url to view the page like this:View variable.aspx

Using Various Data Types

VB.NET and hence the ASP.NET supports a variety of data types such as integer (short and long), decimal (single and double), char, string, date, Boolean, etc. Most of these data types can be used in arithmetic operations, comparison operations, and string concatenations. The following is an example that declares few integer and decimal variables and performs a simple tax calculation. Again, the page load event is used to perform the calculation, and there is no user interaction.

You can use a text editor to copy the code and save the file with a .aspx extension in your web folder. Then use full url to view the page like this:

View tax.aspx

Concatenating Strings

String concatenation is common in an ASP.NET page. The following codes show how strings can be combined or concatenated using & and + signs in two different ways.

Example 1: Using values and & sign

strCombine = “Jack” & “ “ & “Jill”

The result when strCombine is displayed: Jack Jill

Example 2: Using Variables and & sign

Dim firstString As String

Dim secondString As String

Dim twoString As String

firstString = “I am a “

secondString = “graduate student“

twoString = firstString & secondString

The result when strCombine is displayed: I am a graduate student

Arrays

ASP.NET also supports array data type. Thus multiple values of a particular data type can be stored in an array. Note that in VB.NEt array index starts with 0. Thus a 10-member array should be declared as 0-9. The following is an example of an one-dimensional array in which text values are read from three input boxes and are stored in an array. The function “CStr” is used to delete any empty spaces in the input texts and the texts are displayed.

You can use a text editor to copy the code and save the file with a .aspx extension in your web folder. Then use full url to view the page like this:View textboxarray.aspx

Control Structures

Various control structures are used in any programming language such as

  • If.. Then .. Else
  • Select Case
  • For ... Next
  • Do While ...
  • For .... Each

If ... Then ... Else:

Here is an example of “If ... Then ... Else” structure.

View ifthen.aspx

Select Case:

Select Case structure can be simpler than If .. Then .. Else structure when there are many conditions involved. The following is an example of a Select Case structure. The program reads the value from a radio button list and prints appropriate text according to the selection.

You can use a text editor to copy the code and save the file with a .aspx extension in your web folder. Then use full url to view the page like this:

View selectcase.aspx

For ... Next Loop:

Loops are used in programs to repeat an action until a condition is met. The following is an example of For ... Next loop. The programs reads the number of attendees for an event and creates a printable form to complete by the attendees.

You can use a text editor to copy the code and save the file with a .aspx extension in your web folder. Then use full url to view the page like this:

View loop.aspx

Do ... While and Do ... Until Loops:

The For ... Next loop runs for a pre-determined number of times. In many situations, it may not be possible to know that number. The Do ... While loop runs until a condition is fulfilled. Here is an example of Do While ... Loop that repeats until the dice roll is 6 and prints the result. The program first generates a random number and makes some manipulation to convert the dice roll value to an integer. Note how the value of the diceRoll is printed along with the text.

View doLoop.aspx

The following is the same example with the Do Until format.

View doLoop2.aspx

Subroutines and Functions

So far we have used built-in sub routines such as Page_Load that gets executed every time an ASP.NET page is loaded. We can write our own subroutines the similar way, which get executed when they are called within another subroutine or through an user event. Subroutines can also accept inputs. Functions are similar to subroutines. except the name of the function is used to return values from the functions. The following is an example of a function.

View function.aspx

The following example illustrates another use of user-defined subroutine. Program to calculate monthly payment given borrowed amount, interest rate and number of years.

View financialCalculator.aspx
Scope of Variables:

Variables that can be used in multiple subroutines and functions are said to be global in nature. These are declared outside of all subroutines or functions. When a variable is declared within a subroutine or function, its scope is within the subroutine or function only.

The following is an example of a subroutine that shows how subroutines are called from other subroutines and also defines some scopes of variables.

View local.aspx

The following example shows how global variables are declared and how they are used in other subroutines.

View local2.aspx

1