VBScript object

From StealthBot Wiki Backup
(Redirected from Vbscript object)
Jump to: navigation, search

An object is a special value that can be put in a variable. Objects have properties and functions that can only be accessed by using the special . operator.

Setting variables

To store an object into a variable, you must use the Set keyword. If you forget, you receive the vague error "Wrong number of arguments or invalid property assignment" which is sometimes difficult to track down.

Set Obj = OtherObj

This only stores a "reference" to the object; however, that is not important for this discussion.

Where do we get objects?

Other types, including Integer, Boolean, String or Date, all allow us to use literals as values of those types. If we wanted to use other generated values, we'd call built-in functions. But there aren't any literals for type Object. So where do they come from? There are several sources from which you can create objects.

CreateObject

The CreateObject() function allows you to load objects from available ActiveX controls (such as those dependencies required by StealthBot). This is the source of the most common objects used in StealthBot scripting, the Scripting.Dictionary and the Scripting.FileSystemObject.

To create an object:

Set Dictionary = CreateObject("Scripting.Dictionary")

StealthBot objects

StealthBot, as of version 2.7, provides many new ways to access objects that allow you to manipulate StealthBot-related information. These are listed in the list of script objects.

Objects from functions

Sometimes the result of a function (like FSO.OpenTextFile()) is another object.

Custom objects

VBScript allows you to create classes which you can create an object from (see the article for more information).

Accessing the object

Now you have an object. To do something with it, you must know what properties and functions it has.

Properties

A "property" is like a variable. Some properties can be assigned a value, but some cannot. You can retrieve the value of a property in a similar way you do a variable.

Say we have a Scripting.Dictionary. That article lists its properties to include the Count property. It is read-only, but changes when the number of items changes.

Generally, to retrieve a value, use the variable name of the variable storing the object, followed by the . operator followed by the property's name:

Set Dictionary = CreateObject("Scripting.Dictionary")
NumberOfItems = Dictionary.Count

In this case NumberOfItems will hold 0 since the dictionary starts empty.

Another property, the CompareMode property, useful when the keys are going to be compared and you want it to ignore case differences, allows you to assign a value. The same syntax is used as is for assigning to variables, however this changes the behavior of the object Dictionary.

Dictionary.CompareMode = 1

Functions

You may want to read about VBScript functions before reading this section.

A function or subroutine in an object functions the same way as outside of an object. To call it you must know the function's name and number of arguments.

The dictionary object above can be given items by calling the Add subroutine. Like the properties, prefix the name with the object's name and the . operator. In the two ways as mentioned in the VBScript function article.

  1. Call Dictionary.Add("key", "item")
  2. Dictionary.Add "key", "item"

Cleaning up

It is suggested that after an object is done being used, that you clean up the object. To do this, use the only object literal: Nothing, which stands for an empty or null object. Nothing is of type Object, so it is different than Empty, which stands for a variable not set to anything.

Syntax:

Set Obj = Nothing

You can check if something is Nothing with the special Is operator (since checking if Obj = Nothing will cause an error):

If Obj Is Nothing Then
    ' do something when the object is disposed
End If

See also