HTML Tutorials |
|
XML Tutorials |
|
Browser Scripting |
|
Server Scripting |
|
.NET (dotnet) |
|
Multimedia |
|
Web Building |
|
Java Tutorials |
|
Programming Langauges |
|
Soft Skills |
|
Database Tutorials |
|
Operating System |
|
Software Testing |
|
SAP Module |
|
Networking Programming |
|
Microsoft Office |
|
Accounting |
|
|
The For Each...Next Statement |
- For storing one-dimensional sets of data items,collections are similar to arrays.
- An advantage of collections is that items can be indexed both by special keys that are stored along with the data values -- collection.Item("key") and by their locations in the collection -- collection.Item(n).
|
Using Collections for Look-ups
|
A table look-up can be most easily performed with this technique rather than by using arrays,if your data fit the "value" and "key" format of a collection, then .
Returning to a variation on an example presented earlier, the following collection contains state codes (keys) and state names (values).
Then a state code is entered to look up the corresponding state name.
|
Sub Get_State (Src As Object, Args As EventArgs)
Dim TheStates As New Collection
TheStates.Add("Alabama","AL")
TheStates.Add("Alaska","AK")
TheStates.Add("Arizona","AZ")
TheStates.Add("Arkansas","AR")
TheStates.Add("California","CA")
On Error Resume Next
TheState.Text = TheStates.Item( UCase(Code.Text) )
End Sub
<asp:TextBox id="Code" Size="1" MaxLength="2" runat="server"/>
<asp:Button Text="Get State" OnClick="Get_State" runat="server"/>
<asp:Label id="TheState" EnableViewState="False" runat="server"/>
|
|
The For Each...Next Statement
|
Since collections contain related elements within which data are stored, they can also be iterated much like arrays and rather than a For...Next statement, however, a For Each...Next statement is used.
|
For Each item In collection
...item
Next
|
|
The item is a programmer-supplied variable used as the loop's reference to elements in the collection and item must be declared as the same data type as values in the collection.
In the collection,as the loop iterates, item points to each element. Therefore,in the elements item can be used as a reference to the data values stored as they are iterated. An example should make this clear.
Below is repeated the code for the TheStates collection used in the previous example and the script has been changed by adding a For Each...Next loop to iterate the collection and display all the data values in the collection.
|
Sub Show_States (Src As Object, Args As EventArgs)
Dim TheStates As New Collection
TheStates.Add("Alabama","AL")
TheStates.Add("Alaska","AK")
TheStates.Add("Arizona","AZ")
TheStates.Add("Arkansas","AR")
TheStates.Add("California","CA")
Dim Item As String
For Each Item In TheStates
StatesOut.Text &= Item & " "
Next
End Sub
<asp:Button Text="Show States" OnClick="Show_States" runat="server"/>
<asp:Label id="StatesOut" EnableViewState="False" runat="server"/>
|
|
Here, variable Item is used as the index to the collection and it is declared as String because the data items in the collection are strings.In the collection,the loop iterates through each element and each element's contents (Item) is displayed by appending it to the accumulating list.
Notice that the TheStates collection seems to contain two data items in each element -- a state code and a state name. However, recall that the state code is the key to the element value and it is not the value of the element itself.
For many different data structures in Visual Basic,the For Each...Next statement has wide application and there will be several occasions throughout these tutorials to enlist it for use.
|
Trapping for Errors
|
That's all there is to it and except for dealing with possible errors.
If a value is entered for which there is no key match in the collection,then an "index out of bounds" error occurs (the search extended beyond the boundaries of the collection).An On Error Resume Next statement is added immediately before the search for UCase(Code.Text) in order to trap for this error,.
As implied by the statement, if an error occurs while executing this search statement,then simply go on to the next statement; do not stop and display an error message that embarasses the programmer.
Simply check for an error number not equal to 0 in order to trap for this error. Then perform any processing deemed appropriate and you can, for example, add the following code to produce a "No match" message.
|
On Error Resume Next
TheState.Text = TheStates.Item( UCase(Code.Text) )
If Err.Number <> 0 Then
TheState.Text = "No match"
End If
|
|
|
|
Keywords:
while loop statement,
switch case statement,
java statement,
loop statement,
switch statement,
count statement,
function statement,
case statement,
nested statement,
sql statement,
condition statement,
example statement,
else statement,
statement examples
|
|
HTML Quizes |
|
XML Quizes |
|
Browser Scripting Quizes |
|
Server Scripting Quizes |
|
.NET (dotnet) Quizes |
|
Multimedia Quizes |
|
Web Building Quizes |
|
Java Quizes |
|
Programming Langauges Quizes |
|
Soft Skills Quizes |
|
Database Quizes |
|
Operating System Quizes |
|
Software Testing Quizes |
|
SAP Module Quizes |
|
Networking Programming Quizes |
|
Microsoft Office Quizes |
|
Accounting Quizes |
|
|