1

I've been learning the basics of VBScript, and I'm curious as to how UBound and LBound can take one or two arguments. I have read that VBScript supports neither optional parameters nor overloaded functions, so how are these functions able to take the optional second parameter?

This page on the Rhino Developer Docs discusses a workaround for implementing optional parameters in VBscript, but it doesn't seem like that is how UBound and LBound are implemented, since callers don't have to pass in an array of parameters.

Jon G
  • 317
  • 2
  • 7
  • 2
    You are overthinking it. The second parameter is the demension that you want to measure if you are using a multidimensional array. Think a long, long way back in time to when VB1 came along - it only had single dimension arrays so ubound/lbound did not need a dimension param. Then some time later they bring in multi dimension arrays. Oh no - all the existing code that used the ubound/lbound functions would break if they force the need for the second param...so they make the second param optional. End of story. – Vanquished Wombat Feb 11 '19 at 18:25
  • @VanquishedWombat not sure what you are on about but VBScript does not support optional parameters. The closest you can get is passing an array of arguments and handle the argument state yourself. – user692942 Feb 11 '19 at 21:53
  • 1
    @Lankymart - happy to be corrected but just to check....in the case of ubound(arrayvar) is there not a version ubound(arrayvar, dimensionvar) which is the same function name being called with a different number of arguments? And I concur - VBScript does not allow the optional parameter on function arguments. I was thinking of Visual Basic. – Vanquished Wombat Feb 11 '19 at 23:02
  • @VanquishedWombat yes, there is but that is part of the VBScript Runtime and is not supported when creating Functions and Sub Procedures that use the VBScript Runtime. – user692942 Feb 11 '19 at 23:44
  • Possible duplicate of [Is there ways to create optional arguments to functions in vb script?](https://stackoverflow.com/q/1888921/692942) – user692942 Feb 12 '19 at 00:10
  • @Lankymart Thanks for clarifying. That;s probably the answer the OP was seeking. – Vanquished Wombat Feb 12 '19 at 00:17
  • @Lankymart What does it mean that the two versions of UBound are "part of the VBScript Runtime"? Does that mean that UBound is ultimately defined in another language besides VBScript? – Jon G Feb 12 '19 at 14:07
  • @JonG it simply means what runs VBScript, the "Runtime" supports functions with optional parameters but when it comes to writting your own code in VBScript it doesn't. – user692942 Feb 12 '19 at 21:26
  • @Lankymart thank you very much for clarifying. If you'd like to submit an answer with what you said, I can accept it. – Jon G Feb 13 '19 at 13:34

1 Answers1

1

VBScript is about compatibility with typeless VBA (ie not dim as anything).

VBScript error codes are from Altair Basic written by Bill Gates/Paul Allen a million years ago. So it builds on the past. See https://blogs.msdn.microsoft.com/ericlippert/2004/09/09/thirty-years-of-backwards-compatibility/

Take this example

InStr([start, ]string1, string2[, compare])

It tells if the first parameter is a number of not (followed by two strings) to work out if you passed it or not. But you cannot use this to generalise behaviour on other things as this needs to be supported in this specific case for compatibility with VBA (and earlier BASICS).

So your statement I have read that VBScript supports neither optional parameters nor overloaded functions, so how are these functions able to take the optional second parameter? is true except when it isn't.

Noodles
  • 194
  • 1
  • 4
  • It's not typeless VBA it's actually [based off VB6](https://blogs.msdn.microsoft.com/ericlippert/2003/09/15/what-are-the-vbscript-reference-semantics-for-object-members/) *("The fundamental principle that governs this case was "do not be unnecessarily different from VB6." VB6 does the same thing.")* and its own inherent backward compatability. – user692942 Feb 11 '19 at 23:55
  • 1
    VB6 hosts the VBA language. – Noodles Feb 12 '19 at 01:25
  • Eh, no it doesn't. [VBA](https://en.m.wikipedia.org/wiki/Visual_Basic_for_Application) uses the Visual Basic Runtime but cannot run without *usually* being hosted inside another application like Microsoft Office. – user692942 Feb 12 '19 at 01:29
  • 1
    @Lankymart It hosted by the program the VB6 compiler makes. When you press F1 on a VBA keyword in VB6 it takes you to VBA help. When you press F1 on a VB object (Application, Printer, Clipboard, etc) it takes you to VB 6's help. – Noodles Feb 12 '19 at 07:01
  • What are you on about? Just go read up on VB, VBA and the origins of VBScript instead of making claims without any references to back it up. – user692942 Feb 12 '19 at 07:52
  • @Lankymart Start VB6, press F2 for Object Browser, notice how ALL language elements are in the VBA library. – Noodles Feb 12 '19 at 10:34
  • Notice it is exactly the same for Word and Excel. – Noodles Feb 12 '19 at 10:51
  • Because they are "hosting" VBA and VBA is using the Visual Basic Runtime, so what's your point? – user692942 Feb 12 '19 at 11:11
  • The point is that VBA is the language if VB6, Office, AutoCAD, etc. It is not based on anything it is the thing. – Noodles Feb 12 '19 at 19:36
  • Sorry but you are incorrect. You've just confirmed that with that statement, you have it backwards. VB6 is not the "host" for VBA like Office etc. VBA is powered by the VB6 Runtime *(and previous versions before it)*. – user692942 Feb 12 '19 at 20:56
  • Try programming in it instead of mouthing off stuff you don't know. In VB6 press F2 and note there are 4 libraries, StdOLE, VB (the forms package), **VBA (the language)**, and VBRUN what is used to supply runtime objects in the compiled program. – Noodles Feb 12 '19 at 21:01
  • Quote from [Derivative Languages section in Visual Basic (Wikipedia)](https://en.m.wikipedia.org/wiki/Visual_Basic) - *"There are small inconsistencies in the way VBA is implemented in different applications, but it is largely the same language as Visual Basic 6.0 and uses **the same runtime library**."* – user692942 Feb 12 '19 at 21:06
  • You weren't even alive when all these things were hashed out. When Office 97 came out, VBA was put into Word (Excel already had it as it was designed specifically for Excel and variants were developed to hold the contents of an Excel cell). MS advised to use the typeless programming mode - that is where everything is a variant. VBScript is compatable with VBA's typeless mode. – Noodles Feb 12 '19 at 21:13
  • VB1, 2, and 3 were NOT VBA. – Noodles Feb 12 '19 at 21:14
  • What are you on, I never said they were?? You keep suggesting that VBA is underlying language of VB which is just plain wrong. VBA is (as VBScript is) a derivative language of Visual Basic which itself is a derivative of BASIC. – user692942 Feb 12 '19 at 21:15
  • 1
    This is the **language** specification. If you adhere to it then the language is VBA. https://msdn.microsoft.com/en-us/library/dd361851.aspx – Noodles Feb 12 '19 at 21:18
  • Yes, that's the language specification for VBA 7.0, what's your point? VBA is still a language but it IS derived from VB as VBScript is. I'm struggling to see what you are arguing about to be honest. – user692942 Feb 12 '19 at 21:23
  • 1
    It is the SAME not derived. THERE IS NO VB6 HELP available for commands like Instr, MsgBox, etc as they are part of VBA. – Noodles Feb 12 '19 at 21:27
  • You what, seriously fella what are you on about, of course there was? The reason there is no VB help anymore is because Microsoft are useless at storing help files, its getting harder and harder to find VBScript references other than the old .chm files. – user692942 Feb 12 '19 at 21:32
  • 2
    Try it. Press F1 in VB6 on `Instr` - you go to the VBA's help. Press F1 on `form` and you go to VB6's help. Most of my programs host VBScript as a macro language. It's simple to host a language (you just have to pay MS licensing fees to host VBA). – Noodles Feb 12 '19 at 21:48
  • 1
    And both VBScript and VBA are mostly wrappers around functions in oleaut32.dll. So both of them are actually often running the same code such as `VarDateFromStr` that converts a string to a date, either implicitly using `Let` coercion or explicitly with `CDate`. – Noodles Feb 12 '19 at 21:56