O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Choose Function

Class

Microsoft.VisualBasic.Interaction

Named Arguments

No

Syntax

Choose(index, item_1[,item_2, ...[, item_n]])
index (required; Single)

An expression that evaluates to the (1-based) index of the object to choose from the list

item_1-item_n (required; any)

A comma-delimited list of values from which to choose, or a ParamArray containing values from which to choose

Return Value

The object chosen from the list.

Description

Programmatically selects an object from a predefined list of objects (which are passed as parameters to the function) based on its ordinal position in the list. Using Choose is a simpler alternative to populating an array with fixed values.

Rules at a Glance

  • The list of items is based from 1, rather than the more usual VB default base of 0.

  • Because the list consists of objects, you can mix data types within the list; you are not forced to use the same data type for each item in the list. For example, item_1 can be a string, while item_2 can be a long integer, and item_3 can be a floating point number.

  • If the rounded value of index does not correspond to an item in the list, the function returns a null string.

Programming Tips and Gotchas

  • If index is not a whole number, it is rounded before being used.

  • It is important to note that all items in the list are evaluated. Thus, if we use functions or expressions as parameters, all of the functions are called or all of the expressions are evaluated.

  • By providing item_1 through item_n in the form of a ParamArray, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required