Create your own VDK Library
While the VDK comes standard with many libraries that you can use out of the box, it also offers you the capability to write your own libraries for use in your own projects. The below example shows you how to do that.
In the below example, I'm going to build an imaginary mathematics library. If I had to do this without the VDK, I would write some VBScript code (either procedural or OO), put all the code in one VBScript file, save that and execute like so:
intNum1 = 10
intNum2 = 20
Set objAddition = New clsAddition
Set objSubtraction = New clsSubtraction
Set objMultiplication = New clsMultiplication
Set objDivision = New clsDivision
val = objAddition.add2Numbers(intNum1, intNum2)
val = objSubtraction.subtract2Numbers(intNum1, intNum2)
val = objMultiplication.multiply2Numbers(intNum1, intNum2)
val = objDivision.divide2Numbers(intNum1, intNum2)
Class clsAddition
'/**
'A demo class to show how to build your
'own libraries in the VDK.
'
'@author Jason Ely
'/
public function add2Numbers(intNum1, intNum2)
'/**
'Adds 2 numbers and return the results.
'
'@author Jason
'@param intNum1 a numeric integer
'@param intNum2 a numeric integer
'/
If IsNumeric(intNum1) And IsNumeric(intNum2) then
add2Numbers = intNum1 + intNum2
End If
end function
end Class
class clsSubtraction
'/**
'A demo class to show how to build your
'own libraries in the VDK.
'
'@author Jason Ely
'/
public function subtract2Numbers(intNum1, intNum2)
'/**
'subtracts 2 numbers and return the results.
'
'@author Jason
'@param intNum1 a numeric integer
'@param intNum2 a numeric integer
'/
If IsNumeric(intNum1) And IsNumeric(intNum2) then
subtract2Numbers = intNum1 - intNum2
End if
end function
end Class
class clsMultiplication
'/**
'A demo class to show how to build your
'own libraries in the VDK.
'
'@author Jason Ely
'/
public function multiply2Numbers(intNum1, intNum2)
'/**
'multiply 2 numbers and return the results.
'
'@author Jason
'@param intNum1 a numeric integer
'@param intNum2 a numeric integer
'/
If IsNumeric(intNum1) And IsNumeric(intNum2) then
multiply2Numbers = intNum1 * intNum2
End if
end function
end Class
class clsDivision
'/**
'A demo class to show how to build your
'own libraries in the VDK.
'
'@author Jason Ely
'/
public function divide2Numbers(intNum1, intNum2)
'/**
'multiply 2 numbers and return the results.
'
'@author Jason
'@param intNum1 a numeric integer
'@param intNum2 a numeric integer
'/
If IsNumeric(intNum1) And IsNumeric(intNum2) then
divide2Numbers = intNum1 / intNum2
End if
end function
end Class
While this approach will get the job done, you are going to start running into problems later down the line when you want to share code between various scripts. This is where most vbscript programmers start exploring ExecuteGlobal, WSF, WSC or just plain quit VBScript for another language alternative.
With the VDK, you can build your own libraries which can be shared across any vbscript WSH host. I'm going to take the above code and build a VDK mathematics compliant library.
Creating your own library path in VDK HOME
VDK_HOME is the directory in which all VDK resources and libraries live. My VDK_HOME is currently set to "c:\vdk". You can check where your VDK_HOME is by by viewing system environment variables.
When building your own library, you have to decide on your library path in VDK_HOME. I suggest using your organisations web address to create a directory structure. The VDK currently lives on sourceforge so I'll use io.sourceforge. Notice that I reversed the web address. To finish my new directory structure I add "math" to the last part of the path. I now have a directory structure that maps to "%VDK_HOME%\io\sourceforge\math" which looks like the below on my directory system:
Once you have decided on your directory structure, you start creating your class and/or library files. I've taken the above code and put each class into a separate .vbs file with the file name being the name of the class.
If you have VDK Doc Viewer installed, you can now see the new libraries as in the below image.
Now that your libraries are built, you can reference them in a external script like so:
'Start VDK
ExecuteGlobal CreateObject("wsh.vdk.initialize").initialize
'import my math libraries that I just created
import "io.sourceforge.math.clsAddition"
import "io.sourceforge.math.clsDivision"
import "io.sourceforge.math.clsMultiplication"
import "io.sourceforge.math.clsSubtraction"
'after I have import my libraries, I can create objects
Set objAddition = New clsAddition
Set objSubtraction = New clsSubtraction
Set objMultiplication = New clsMultiplication
Set objDivision = New clsDivision
intNum1 = 10
intNum2 = 20
'Call your custom methods on the classes you just created
val = objAddition.add2Numbers(intNum1, intNum2)
val = objSubtraction.subtract2Numbers(intNum1, intNum2)
val = objMultiplication.multiply2Numbers(intNum1, intNum2)
val = objDivision.divide2Numbers(intNum1, intNum2)
If you want to try this out yourself, you can download the DEMO project. To run the demo, do the following:
- Unzip the project to your VDK HOME directory.
- Copy the above code into a .vbs file and save.
- Execute the .vbs file or open the file in your favourite vbscript IDE and execute.
Share the VDK love!
I like tossing around 1's and 0's just like the next programmer but what I also like, is sharing my code so that other people can use it (free of charge!). The VDK promotes open source sharing so if you have written your own library, please share it with the rest of us by zipping it up and making it available as a download.