This has really taken my time to find out just why always the message box display "two"?. Can anybody tell me why?
Private Sub Command1_Click()
Dim s As String
s = Choose(3.4, "zero", "one", "two", "three", "four")
MsgBox s
End Sub
Printable View
This has really taken my time to find out just why always the message box display "two"?. Can anybody tell me why?
Private Sub Command1_Click()
Dim s As String
s = Choose(3.4, "zero", "one", "two", "three", "four")
MsgBox s
End Sub
Code:
s = Choose(3.4, "zero", "one", "two", "three", "four")
The choose function rounds out numbers and the first index,in your case 3.4 = 3. It therefore chooses your third choice which is two
index = 3.4
1st = zero
2nd = one
3rd = 2
4th = 3
5th = 5
if you change your index to 1 it will return zero and so on
3.4 is rounded to 3, and Choose first alternative is zero (it's option base is 1) so the third is "two"
Hi,
It's because the elements in the Choose function start of at one. ie: Zero = element(1), one = element(2) etc..
Choose also rounds down on it's search.
Therefore your code will place the 3rd element (3.4 rounded down), "two", into String s.
Hope this helps
Shaun
[Cross posted..sorry Kedaman & HeSaidJoe)
Actually the Choose function doesn't care about what you put in Option Base - it's always one-based. :rolleyes:
I know but so is Mid$, Instr$, Right$, Left$, Put# Get#, Seek and a lot of other functions, some controls and classes, the collection object, treeview, listview are typical optionbase 1 stuff