Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 22 / 68

Searching an array (Array.IndexOf)

This lesson covers searching an array with VB.NET's Array.IndexOf, with the aim of checking whether a value is present.

Array.IndexOf(array, value) tells you whether the value is in the array and, if so, where. It returns the position (counting from 0) when found and -1 when not.

The sample code confirms a value is present because Array.IndexOf(fruits, "orange") returns 0 or more, while Array.IndexOf(fruits, "banana") returns -1 so the >= 0 test comes out false.

A common early stumble is forgetting that the not-found result is -1. Writing a bare If Array.IndexOf(...) cannot distinguish that from a match at position 0, so the comparison with >= 0 is needed.

In professional work, this search comes up whenever you check whether a list contains a particular product ID or user name.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)