Having gone through the Introductory part, it is time to look at some crucial fundamentals and concepts. This article is to refresh some of the key facts and features of VBScript. In the least, you should consider the following six points for developing a fundamental understanding of VBScript.
Though we will cover these concepts in depth along with examples in the subsequent tutorials, but for now these brief points are enough for you to understand ‘what you can or should do with VBScript’. So without further ado, let’s jump on to these concepts.
VBScript is a Scripting Language
‘VBScript’ is a ‘scripting language’! What does this mean? Well, a vague difference between a programming and scripting is that a scripting language is not pre-compiled until runtime and hence while the end product of a programming language is mostly a compiled binary executable program (example; .exe), the end product of a scripting language is rather the source code itself (example; .vbs file)!
Hence unlike C++, VB or other compiled programming languages, the ‘VBScript’ is essentially an ‘interpreted language’ which is interpreted by ‘VBScript Engine’ at runtime i.e. every time the code is actually executed it is interpreted (or compiled internally).
For detailed explanation around interpreted and compiled languages, please refer to the earlier article ‘Interpreted Vs Compiled languages‘
VBScript Supports ‘Late Binding’
Since VBScript is a not a compiled language, hence natively it supports only ‘Late Binding’. Therefore unlike the ‘Early Binding’ scenario, in case of VBScript being the ‘Late Binding’ language, the binding does not occur until the program is actually executed.
In this scenario the program usually performs frequent registry or namespace lookups upon execution to access the information about the ‘COM Object’, consequently, any issues with the COM information are not discovered until actual execution. For further reading around this concept, please refer to dedicated article on ‘Early Bound Vs Late Bound‘
Loosely Typed Language
When it comes to ‘Data Types’, VBScript is a ‘loosely typed language’. What this means is that the programmer does not have to declare the ‘Data Type’ of variables in advance. Stating it the other way, VBScript does not provide any means for declaring the ‘Data Types’ for the variables and rather uses a generic or universal data type called the ‘Variant’ which can logically hold any type of value by means of its ‘Sub-Type’
Such a strategy of ‘having and not having data type’ simultaneously is manifested by the concept of ‘Variant’ and its ‘Sub-Type’. Summing-up this by means of a vague analogy, “all variables in VBScript have a default ‘data type called variant’ which is equivalent of ‘no data type’, however, each variant has an associated ‘Sub-Type’ which could be a string, integer, float” etc. Together they manifest the ‘having and not-having’ strategy. More details around this have been discussed in the below section
Single and Universal Data Type
As discussed above, VBScript has only one Data Type which is called the ‘Variant’. This ‘Variant’ is a special type of data type in the sense that it can logically hold almost any type of value e.g. String, Integer, Date, time etc. But this does not mean the VBScript is totally unaware about ‘Data Types’, in order to facilitate VBScript with the intelligence to recognize and deal with ‘Data Types’ it has a special feature known as the ‘SubType’. This ‘SubType’ is kind of ‘Data Type’ of the ‘Variant’ itself. For better understanding Imagine this :
When you declare a variable it automatically gets the ‘Variant’ data type assigned to it. But when you actually put some value in this variable then VBScript assigns the appropriate Sub Type (data type) for this value. You do have means to force the data types or query the data types from there onwards.
So in VBScript, you can solve the dilemma of Data Types by asking the ‘Variant’ that, what is it’s SubType. Now, this might complicate things further hence we will leave it here on the worth mentioning note that – the ‘Variant’ is the only data type supported by VBScript and hence it is the only data type returned by VBScript functions.
Object Model and Libraries
VBScript does not have any proprietary object model of its own! That is to say that if you want to access say for example ‘FileSystem’ or ‘Network’ objects and namespace, VBScript alone cannot help you. It has no way to directly talk to these resources.
But the best part is, that it can hook to most of the COM Components or Servers and exploit them to get the work done. Hence whether you need to play with databases (ADO) or XMLs (DOM) or HTML (DOM) or any other supported COM stuff, it’s a piece of cake for VBS!
Apart from being such a COM Master, it has got some very cool good friends from the same Microsoft family; namely the ‘WSH’, ‘WMI’ and ‘Scripting Runtime Library’. You can literally get almost any scripting or automation task done on windows if you are able to make VBScript work together with these friends.
COM Enabled and COM Automation
On the last note, VBScript is a fully ‘COM-Enabled’ scripting language and hence can very well work with a variety of COM Components / Servers. The worth mentioning point is that VBScript works only with the COM Objects which implement an ‘IDispatch’ interface.
Moreover, VBScript supports COM scripting which is to say that you can create your own ‘Custom Classes’ and their methods and properties in VBScript. This gives you increased power while using VBScript. By coding ‘classes’ you can make your code more robust, modular and reusable.