Set Command

<< Click to Display Table of Contents >>

 

Set Command

VBScript implements the concept of polymorphism from object-oriented languages, allowing a Variant-type variable to assume the form of any object via the Set command. This way, a variable works as a pointer to an object, allowing the access to its methods and properties, according to the next example.

Sub CommandButton1_Click()
  Set E3Chart = Screen.Item("E3Chart1")
  E3Chart.Pens.Item(2).Color = RGB(212, 208, 20)
End Sub

 

In this example, the same task of the previous topic was accomplished, but the part related to retrieving the specific object was omitted. Without the Set command, the same call must be written according to the next example.

Screen.Item("E3Chart1").Pens.Item(2).Color = RGB(212, 208, 20)

 

Apparently, there is no advantage in this case, because users can perform that with a single line of code. But, if they want to perform other operations in the same script right after, the process is simpler and faster if the call to the Item method is not in all lines.

Sub CommandButton1_Click()
  'A bad example
  Screen.Item("E3Chart1").Pens.Item(0).Color = RGB(212, 208, 20)
  Screen.Item("E3Chart1").Pens.Item(1).Color = RGB(200, 208, 20)
  Screen.Item("E3Chart1").Pens.Item(2).Color = RGB(100, 208, 20)
End Sub
 
Sub CommandButton1_Click()
  'A better example
  Set Pens = Screen.Item("E3Chart1").Pens
  Pens.Item(0).Color = RGB(212, 208, 20)
  Pens.Item(1).Color = RGB(200, 208, 20)
  Pens.Item(2).Color = RGB(100, 208, 20)
End Sub

Was this page useful?