C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: In the second usage of the Choose function, we use the Random type to randomly select one of the arguments to Choose.
RandomInfo: The arguments to Random.Next are inclusive and exclusive, so we pass 1 and 4. Choose returns a random element of the 3 that were passed.
Array: Instead of specifying the arguments to Choose directly, you can encapsulate them in an array type.
And: This makes for more extensible code. You can add as many elements as you want to the array and then choose from them.
VB.NET program that uses Choose function
Module Module1
Sub Main()
' Get the third choice in the list of choices.
Dim result As String =
Choose(3, "Dot", "Net", "Perls", "Com").
ToString
Console.WriteLine(result)
' Get a random choice from the three choices.
Dim rand As Random = New Random()
result =
Choose(rand.Next(1, 3 + 1), "Visual", "Basic", "NET").
ToString
Console.WriteLine(result)
End Sub
End Module
Output
Perls
NET
Also: It will return Nothing if you pass an index that is out of range. This is a null value.
Nothing