-1

The following throws an error with or with out Option Explicit set.

Create a file named dim.vbs.

Dim a as String

Then run at the command prompt.

cscript dim.vbs

Microsoft VBScript compilation error: Expected end of statement

Is there a way to force the variable type with or with out Option Explicit set?

Lime
  • 12,724
  • 9
  • 48
  • 84
  • @BigBen how do I run VBA from the command line then? – Lime Aug 21 '20 at 03:28
  • 1
    You don't. VBA is hosted. – BigBen Aug 21 '20 at 03:28
  • 1
    VBA is not VBScript. They're very different languages. VBScript is a front-end language for the ActiveScripting engine and does not support typed variables. – Dai Aug 21 '20 at 03:28
  • `Dim a` is the *only* thing you can do in Vbscript. It only has one data type. – BigBen Aug 21 '20 at 03:28
  • 1
    @BigBen VBScript does have data-types, they're just not exposed to the programmer. – Dai Aug 21 '20 at 03:29
  • 2
    @Dai - yeah semantics shemantics lol, point taken. "One data type" is sloppy on my part. – BigBen Aug 21 '20 at 03:29
  • @BigBen if I add OptionExplicit it seems to run vba code seamlessly. Back to the question is there a way to fix this? – Lime Aug 21 '20 at 03:33
  • 1
    @Lime Please post the VBA code that you say runs "seamlessly" in VBScript. – Dai Aug 21 '20 at 03:35
  • 4
    ^ Clearly `Dim a As String` *doesn't* work in VBScript so the claim that VBA code "runs seamlessly" must mean you either didn't declare your variables or declared them without a type and as such you "got lucky." As mentioned by @Dai, VBscript does not support typed variables, which answers your question. – BigBen Aug 21 '20 at 03:35
  • @BigBen you are right. Sigh Is there a guide to converting VBA to VBScript or automated way it would be nice. – Lime Aug 21 '20 at 03:44
  • 2
    @Lime Semantic-preserving automatic program conversion is a holy-grail of computer-science and software engineering (especially inter-paradigm automatic program conversion) - if you do find or build such a tool you would become very rich and famous overnight. – Dai Aug 21 '20 at 03:49

2 Answers2

5

How do I declare a variable type in vbscript?

You can't.

  • VBScript does not support the concept of statically-typed variables.
  • VBScript is a frontend language for Windows' ActiveScripting environment.
    • This environment is hosted by cscript (for the command-line) and wscript (when you don't want a CMD window to appear).
    • The environment is also hosted by IIS for Classic ASP .asp/.asa, and formerly Internet Explorer (prior to Internet Explorer 9) where it was the basis for its JavaScript engine.
      • "JScript" is the brand (or dialect) of JavaScript also supported by ActiveScripting: both VBScript and JScript share similarities in what they're both capable of.
  • ActiveScripting is intended for working with COM objects.

VBScript's syntax is a subset of VBA's and largely mutually intelligible because VBA will implicitly type variables without a static type definition as a COM Variant object, which is roughly equivalent (and possibly identical, I forget) to the real underlying type used by VBScript's variables.

(Internally VBScript is typed: otherwise it would be impossible for VBScript to use COM. You can tell there's some typing under-the-hood because of how you need to use the Set and Let keywords based on the expected type of a value, or how you can only use Const in VBScript with certain literal value types, and so on).

By analogy: you have a driver's license for a passenger-car (you know how to write VBA) and you decide to drive a forklift truck (VBScript) - because you know how to drive a passenger-car and because the controls/interface looks the same (it's got a steering wheel, a handbrake, a driver's seat, a speedometer) that your skills as a passenger-car driver mean you should be able to drive a forklift truck immediately and without any problems. You can't and you won't: forklift trucks handle very differently (e.g. rear-wheel steering, electric drive, and many regions require specialty licensing for forklift truck drivers for this reason).

Dai
  • 110,988
  • 21
  • 188
  • 277
  • 3
    That analogy though. Spot on, made my evening :-) Sounds like Michael from The Office. – BigBen Aug 21 '20 at 03:45
  • "Internally VBSCript is typed" - hence `TypeName` also, besides the examples you give. – BigBen Aug 21 '20 at 03:49
  • 1
    @BigBen Correct. It's VBScript's equivalent to JavaScript's `typeof` operator. – Dai Aug 21 '20 at 03:50
  • @Lime The other answer that you accepted just now does not contradict anything in my answer. Their answer shows that VBScript does have value-typing *internally* (which I said too!) but their answer goes into more detail, but it does not answer your question any more than mine does - and that answer still agrees with me on the main point in your question which is that VBScript does **not allow you to declare variables with a static type**. – Dai Aug 26 '20 at 11:45
  • This appears to not tell the whole story* – Lime Aug 26 '20 at 11:46
  • 1
    @Lime If you had asked how VBScript's typing system worked in your question I would have provided the same detail. I agree the other answer provided useful insight and examples, but neither my answer nor theirs provides the "whole story". Your question was very short and light on details - I feel you're being arbitrary. – Dai Aug 26 '20 at 11:49
  • 1
    Tbh @dai, shouldn't have been answered full stop and should have been [flagged as a duplicate](https://stackoverflow.com/questions/13855633/vbscript-error-when-declaring-any-variable-with-its-required-data-types). I personally upvoted your answer because it's a very technical detailed answer, unfortunately, it's wasted here. Maybe re-post it on the dup target? – user692942 Aug 26 '20 at 12:26
  • @Lankymart If I marked this question as a duplicate I wouldn't have earned the +4 votes I've got so far :D (btw, I have also upvoted your answer (as did someone else), but 2 other people downvoted you, that's why your answer is at zero)). Also it's 5:52am here, I don't want to think about it :D – Dai Aug 26 '20 at 12:53
  • 1
    @Lankymart Derp. I just realised you aren't brandongoodman - nvm – Dai Aug 26 '20 at 12:56
0

Although its not possible to declare variables with specific types in VBS as you would in VB or VBA, there are several conversion functions that will ensure that your variable contain the data type that you desire.

CBool, CByte, CCur, CDate, CDbl, CInt, CLng, CSng, CStr

for instance

Option Explicit
Dim x
x = "1"
WScript.Echo TypeName(x)
x = CInt("1")
WScript.Echo TypeName(x)
x = CInt(1)
WScript.Echo TypeName(x)

produces

String
Integer
Integer

Just beware that passing unconvertable values into a conversion function may cause Type mismatch errors. For instance, a string like "x9x" cannot be converted to a number so CInt("x9x") will cause a runtime error that halts execution.