Results 1 to 6 of 6

Thread: why always "two"?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    New Delhi, India
    Posts
    75

    Unhappy

    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

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    3.4 is rounded to 3, and Choose first alternative is zero (it's option base is 1) so the third is "two"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    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)
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Actually the Choose function doesn't care about what you put in Option Base - it's always one-based.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width