Search | Contact | Link To Us  

      ASP.NET      

ASP.NET
Dev Environment
Web Forms
Web Controls
ADO .NET
Web User Controls
Deployment
XML Web Services
ASP.NET Feedback Form
Resources
Discussions
VB .NET
About



Advertisement
































































     HomeASP.NETWeb Forms> Arrays, Strings 

Arrays, Strings

Arrays

An array is a way of declaring a whole group of variables at once. Arrays are programming constructs that store data and allow us to access them by numeric index or subscript. Arrays helps us create shorter and simpler code in many situations. Arrays are based on the System.Array namespace. They are declared using Dim, ReDim, Static, Private, Public and Protected keywords. An array can have one dimension or more than one. The dimensionality of an array refers to the number of subscripts used to identify an individual element.

An array declaration looks as follows: Dim Sports (5) as String. This single line creates a Sports array that has 6 elements starting from Sports(0) to Sports(5). The first element of an array is always referred by zero index. To change the dimension (redimension) of the Sports array which we already declared we use the following statement: ReDim Sports(20). The size of Sports array changes from 11 to 21.

Arrays Sample

The following sample will demonstrate arrays. Drag a button and a textbox control on to the Web Forms page. Open the code designer window and paste the following code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim Sports(5) As String
Sports(0)="Tennis"
Sports(1) = "Cricket"
Sports(2) = "Rubgy"
Sports(3) = "Aussie Rules"
Sports(4) = "Soccer"
Sports(5) = "Hockey"
Label1.Text = "The sport in the location you entered is" & " " &_
Sports(CInt(TextBox1.Text))
End Sub

The above sample code creates a Sports array with six elements, asks the user to enter a number between 0 and 5 and displays the sport that is stored in the location (number) the user entered on a label when the button is clicked. Test the code at the bottom of this page for understanding.

Strings

Strings are supported by the .NET String class. The String data type can represent a series of characters and can contain approximately up to 2 billion Unicode characters. There are many built-in functions in the String class. Some .NET Framework functions are also built into the String class. Some common String functions are discussed below:

LCase: Converts a string to lower case
UCase: Converts a string to upper case
Len: Calculates the number of characters in the string
LTrim: Omits spaces that appear on the left-side of the string
RTrim: Omits spaces that appear on the right-side of the string
Trim: Omits both leading and trailing spaces
Clone: Clones a string
Concat: Joins two strings
Space: Used to create a string with spaces
Left: Takes two arguments and returns a string that consists of the left-most characters of the string sent
Right: Takes two arguments and returns a string that consists of the right-most characters of the string sent
Instr: Searches and returns a shorter string within a long string
Replace: Seraches for a small string in a long string and replaces it with the specified string

Sample Application

The following is a simple and interesting sample. It asks the user to enter some information and generates a username and password based on the information entered and displays the newly generated login information to the user. To start, drag six labels, four textboxes and a button control on to the Web Forms page. TextBox1 accepts first name, TextBox2 accepts last name, TextBox3 accepts age and TextBox4 accepts country. Look at the sample below for the user interface. Open the code designer window and paste the following code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim mystr As String
Dim mystr1 As String
Dim mystring As String
Dim midd As String
'declaring three strings
mystr = Left(TextBox1.Text, 3)
'using the left function
mystr1 = Right(TextBox2.Text, 3)
'using the right function
midd = Mid(TextBox4.Text, 2, 6) & CInt(TextBox3.Text) - 11
'using the mid function
mystring = mystr & mystr1 & TextBox3.Text
Label5.Text = "Your user name is" & " " & mystring
Label6.Text = "Your random password generated by our server is" & " " & midd
TextBox1.Visible = False
TextBox2.Visible = False
TextBox3.Visible = False
TextBox4.Visible = False
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
End Sub

Live Code Demo


Arrays

Enter a number between 0 and 5 to get the Sport stored in that location

 

Strings

First Name               

Last Name                

Age                         

Enter Country           

            


  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2010 Startvbdotnet.com. All rights reserved.