29

What is the purpose of the colon?

e. g.:

Dim objConn : Set objConn = OpenConnection()`

Is the colon used to combine the two statements into one line? I just want to be sure.

ruffin
  • 16,507
  • 9
  • 88
  • 138
burnt1ce
  • 14,387
  • 33
  • 102
  • 162

4 Answers4

23

Yes, the code would work exactly the same on two lines; the colon's just a statement separator.

Eifion
  • 5,403
  • 2
  • 27
  • 27
  • Then why is this not working ?? `Function Pad(s) : Pad = Right("00" & s, 2) : End Function`. It's throwing `syntax error` on that same line.. – Vicky Dev Oct 21 '21 at 07:46
  • @VickyDev That's a new question -- though, just for fun, it does work for me (though it may not do what you intended). `Function Pad(s) : Pad = Right("00" & s, 2) : End Function : str = Pad("spam")` gives me `am` in `str`, which seems right -- no errors. **But try asking a _new_ question and see if someone can help.** – ruffin May 18 '22 at 17:48
20

You can put two (or more) lines of code in one line. It's most often used, as in your example, to declare and set a variable on one line.

Think of it like a semicolon in every other language, except optional.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
9

Yes this is correct. In VB style languages, including VBScript, the colon is an end of statement token. It allows you to place several statements on the same line.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Then why is this not working ?? `Function Pad(s) : Pad = Right("00" & s, 2) : End Function`. It's throwing `syntax error` on that same line.. – Vicky Dev Oct 21 '21 at 07:46
6

What you have stated is correct. The purpose of the colon is to combine 2 otherwise separate lines into a single line. It works on most statements, but not all.

witttness
  • 4,984
  • 4
  • 25
  • 24