19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The ContainsIndex() method takes two arguments. The first argument is<br />

the name of the array, and the second argument is the index you’re checking<br />

for. The method returns "True" or "False" depending on whether the<br />

index exists in the array.<br />

Line 6 shows that searching for the index is case insensitive, which is why<br />

the search for the index homer returns "True". Also, searching the score array<br />

for index "1" (as a string) or index 1 (as a number) both returned "True".<br />

If you’re not sure whether an array includes a particular index, you can<br />

use the ContainsIndex() method <strong>to</strong> find out. This method is especially helpful<br />

if you’re working <strong>with</strong> very long arrays.<br />

Does It Have a Particular Value?<br />

The Array object also offers a method that checks whether an array contains<br />

a certain value. Run the program in Listing 16-7 <strong>to</strong> discover how the<br />

ContainsValue() method works.<br />

1 ' ContainsValue.sb<br />

2 age["Homer"] = 18<br />

3 age["Marge"] = 17<br />

4 score[1] = 90<br />

5 ans1 = Array.ContainsValue(age, 18) ' Returns "True"<br />

6 ans2 = Array.ContainsValue(age, 20) ' Returns "False"<br />

7 ans3 = Array.ContainsValue(score, 90) ' Returns "True"<br />

8 TextWindow.WriteLine(ans1 + ", " + ans2 + ", " + ans3)<br />

Listing 16-7: Demonstrating the ContainsValue() method<br />

The ContainsValue() method returns "True" or "False" depending on<br />

whether the value it checks for exists in the array.<br />

Note<br />

Unlike the ContainsIndex() method, the ContainsValue() method is case sensitive.<br />

So it’s best <strong>to</strong> be consistent <strong>with</strong> your casing!<br />

Give Me All the Indices<br />

Another useful method of the Array object is GetAllIndices(). This method<br />

returns an array that has all the indices of a given array. The first element<br />

of the returned array has an index of 1. To understand how this method<br />

works, run the program in Listing 16-8.<br />

1 ' GetAllIndices.sb<br />

2 age["Homer"] = 18<br />

3 age["Marge"] = 17<br />

4 names = Array.GetAllIndices(age)<br />

5 TextWindow.WriteLine("Indices of the age array:")<br />

234 Chapter 16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!