The Daily Newbie - Using the Choose() Function
Posted: 2002-06-01
By: ArchiveBot
Viewed: 79
Filed Under:
No attachments for this post
04/28/2001 - Describes the usage of the Choose() function; A really neat but seldom used command in VB.
Original Author: Matt Roberts
Code
The “To Start Things Fourth About this Today's command is not widely known for some reason, but is faily useful. face="Arial">Today’s Keyword:
content="text/html; charset=iso-8859-1">v:shapes="_x0000_s1027">
Daily Newbie
Off Right”
Edition
April 28,
2001
Freev:shapes="_x0000_s1027">
feature:
The initial plan for the Daily Newbie was to cover each function VB has to offer
in alphabetical order. I have now modified this plan slightly to skip over some of
the more advanced (or tedious) commands that I don't think the Newbie would benefit from.
Thanks again all who have written in support of this effort. It makes a difference.
I have been guilty of writing functions that do the exact same thing several times. I think you will
like this one.
size="4" face="Arial"> Choose()
face="Arial">Name Derived
From:
size="2" face="Arial">Choose (of
course) – “(1) : to make a selection"
- Webster's online
dictionary.
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Used for
Making a choice between several possible options.
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">VB Help Description: Selects and returns a value from a list ofarguments.
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Plain
English: Returns the option associated with the value passed it (I will just have to show you!)
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Syntax: Choose(index, Choice1, Choice2, etc...)
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Usage: strDecision = Choose(intChoice , "Just do it" , "Don't do it" , "It's your life" )
style="margin-left:135.35pt;text-indent:-135.35pt">face="Arial">Copy & Paste Code:
Today's code snippet will prompt for a month number and return a string
that corresponds to it.
Dim Choice
Dim strMonth As String
Do
Choice = Val(InputBox("Enter a Number (1-12):"))
If Choice + 0 = 0 Then Exit Do
strMonth = Choose(Choice, "Jan", "Feb", "Mar", "Apr", _
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
MsgBox strMonth
Loop
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Notes:
I really like the Choose function. It comes in handy for with evaluating which option button
was clicked, or anything else that returns an index number. Unfortunatly, like the Array() command
covered a couple of articles ago, the Choose() function requires a seperate hard coded value for
each possible choice. This isn't neccesarily a bad thing, but I am allergic to hard coding, so it
just rubs me wrong. I guess the chances of the order of the months changing is pretty slim...
Things to watch out for:
it acts like a compact series of If...Then statements. This can result in the sometimes baffling behavior
of displaying one message box with the correct value and many empty ones. For this reason, the results
of a Choose() statement should be returned to a variable before displaying it in a message box.
as your Index, you should add zero to it to initialize it as a number. This will make the default value zero, not null.
Tomorrow's Keyword: Chr()
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.