简体   繁体   中英

Single line VBScript Select Case with multiple statements: eg: Case 0: x=a y=b

I'm randomly selecting a flag and corresponding country using VBScript. Because of the way I'm generating the code (programmatically), each Case statement will end up on a single line.

I know to use the colon (":") after the Case x: to keep the flag var on the same line. Adding the country var to the line breaks the code though.

What is the proper syntax for this line?

Current code sample:

Select Case rndCountry
Case 0: flag="af" country="Afghanistan"
Case 1: flag="al" country="Albania"
Case 2: flag="dz" country="Algeria"
Case 3: flag="as" country="American Samoa"
Case 4: flag="in" country="Andaman Islands"
Case 5: flag="ad" country="Andorra"
Case 6: flag="ao" country="Angola"
Case 7: flag="ai" country="Anguilla"
Case 8: flag="aq" country="Antarctica"
Case 9: flag="ag" country="Antigua and Barbuda"
Case 10: flag="ar" country="Argentina"
Case 11: flag="am" country="Armenia"
Case 12: flag="aw" country="Aruba"
Case 13: flag="ac" country="Ascension Island"
End Select

The above code yields the "Expected end of statement" error.

No sooner do I post than I figure it out:-)

The correct syntax is:

Case 0: flag="af": country="Afghanistan"

Note that the following also works:

Case 0: flag="af": country="Afghanistan":

In short, colons are needed after each variable to keep everything on one line of code.

VBScript uses new lines to determine where one statement starts and another one begins, but you can use a colon to terminate a statement instead which allows you to span multiple statements across one line.

For example:

Case 0: flag="af": country="Afghanistan"

Is the equivalent of:

Case 0
  flag="af"
  country="Afghanistan"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM