Reventlov
2015-08-29 12:40:50 UTC
I googled a while looking for new Active/x objects that can be instanced from VbScript.
Most of the .NET objects seem to be in DLLs that can be called with other methods but not
with CreateObject or whatever vb.net uses to connect to its objects.
I have found these examples:
Some sort of Dictionary with sorting, search, remove, insert.
A string builder with inser/replace methods
A LIFO and a FIFO stack
Any others?
Set DataList = CreateObject ("System.Collections.ArrayList")
DataList.Add "B"
DataList.Add "C"
DataList.Add "E"
DataList.Add "D"
DataList.Add "A"
DataList.Sort()
'Datalist.Reverse() 'Reverse order
'DataList.Remove("D") 'Removes an element
For Each strItem in DataList
Wscript.Echo strItem
Next
'*******************************************************************************
'Random number between 1 and 100 (1 included, 100 aparently not included)
Set objRandom = CreateObject("System.Random")
Wscript.Echo objRandom.Next_2(1,100)
'*******************************************************************************
'Concatenate strings. Faster than using "&"
set wrt = CreateObject("System.IO.StringWriter")
for i = 1 to 100000
s = CStr(i)
wrt.Write_12 s 'String. Write_11 for decimal, _10 for double, _9 for float
next
s = wrt.GetStringBuilder().ToString()
'*******************************************************************************
set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(1, "loneliest")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(2, "happiest")
WScript.Echo sb.ToString()
'Prints:
'1 is a loneliest number
'2 is a happiest number
'*******************************************************************************
Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."
s.Append_3 "and the rest."
wscript.echo s.Length
wscript.echo s.Capacity
wscript.echo chr(s.chars(0)) 'returns first char
wscript.echo s.Replace("t", "d").Replace("l", "k").toString
s.Insert_2 7, "insert this " 'after char 7, starting from 1
WScript.Echo s.tostring()
WScript.Echo s.remove(10,4).tostring() 'Removes 4 chars starting from position 10
WScript.Echo s.tostring()
s.clear 'Removes all characters from the stringbuilder instance
'*******************************************************************************
'A sorted dictionary object
'http://www.reliance-scada.com/en/support/articles/technical/vbscript-tip-working-with-an-object-list
Dim Message, SortedList, i, s
' Initializes the message.
Message = ""
' Creates a list.
Set SortedList = CreateObject("System.Collections.SortedList")
' Fills the list.
SortedList.Add "Point", 1
SortedList.Add "Point Cloud", 2
SortedList.Add "Curve", 4
SortedList.Add "Surface", 8
SortedList.Add "Polysurface", 16
SortedList.Add "Mesh", 32
' Returns the element count.
Message = Message & "1: " & SortedList.Count & vbCrLf
' Returns the value by key.
Message = Message & "2: " & SortedList("Surface") & vbCrLf
' Returns the value at the index.
s = ""
For i = 0 To SortedList.Count - 1
s = s & CStr(SortedList.GetByIndex(i)) & " "
Next
Message = Message & "3: " & s & vbCrLf
' Verifies the existence of the key.
Message = Message & "4: " & SortedList.ContainsKey("Polysurface") & vbCrLf
' Verifies the existence of the value.
Message = Message & "5: " & SortedList.ContainsValue(16) & vbCrLf
' Returns the index of the key (searches for the element by key).
Message = Message & "6: " & SortedList.IndexOfKey("Polysurface") & vbCrLf
' Returns the index of the value (searches for the element by value).
Message = Message & "7: " & SortedList.IndexOfValue(16) & vbCrLf
' Removes the element by key.
SortedList.Remove "Polysurface" & vbCrLf
' Removes the element at the specified index.
SortedList.RemoveAt 0
' Removes all elements.
SortedList.Clear
' Releases the list.
Set SortedList = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
'*******************************************************************************
'LIFO Last In First Out stack
Dim Message, Stack, Item, s
' Initializes the message.
Message = ""
' Creates a list.
Set Stack = CreateObject("System.Collections.Stack")
' Fills the list.
Stack.Push "Item_1"
Stack.Push "Item_2"
Stack.Push "Item_3"
Stack.Push "Item_4"
' Goes through the list elements.
s = ""
For Each Item In Stack
s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf
' Converts the list into an array.
Message = Message & "2: " & Join(Stack.ToArray, ",") & vbCrLf
' Returns the element count.
Message = Message & "3: " & Stack.Count & vbCrLf
' Verifies the existence of the value.
Message = Message & "4: " & Stack.Contains("Item_2") & vbCrLf
' Removes and returns the last element.
Message = Message & "5: " & Stack.Pop & vbCrLf
' Returns the last element (without removing it).
Message = Message & "6: " & Stack.Peek & vbCrLf
' Empties the list.
Stack.Clear
' Releases the list.
Set Stack = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
'*******************************************************************************
'FIFO first in first out queue
Dim Message, Queue, Item, s
' Initializes the message.
Message = ""
' Creates a list.
Set Queue = CreateObject("System.Collections.Queue")
' Fills the list.
Queue.Enqueue "Item_1"
Queue.Enqueue "Item_2"
Queue.Enqueue "Item_3"
Queue.Enqueue "Item_4"
' Goes through the list elements.
s = ""
For Each Item In Queue
s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf
' Converts the list into an array.
Message = Message & "2: " & Join(Queue.ToArray, ",") & vbCrLf
' Returns the element count.
Message = Message & "3: " & Queue.Count & vbCrLf
' Verifies the existence of the value.
Message = Message & "4: " & Queue.Contains("Item_2") & vbCrLf
' Removes and returns the first element.
Message = Message & "5: " & Queue.Dequeue & vbCrLf
' Returns the first element (without removing it).
Message = Message & "6: " & Queue.Peek & vbCrLf
' Empties the list.
Queue.Clear
' Releases the list.
Set Queue = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
Most of the .NET objects seem to be in DLLs that can be called with other methods but not
with CreateObject or whatever vb.net uses to connect to its objects.
I have found these examples:
Some sort of Dictionary with sorting, search, remove, insert.
A string builder with inser/replace methods
A LIFO and a FIFO stack
Any others?
Set DataList = CreateObject ("System.Collections.ArrayList")
DataList.Add "B"
DataList.Add "C"
DataList.Add "E"
DataList.Add "D"
DataList.Add "A"
DataList.Sort()
'Datalist.Reverse() 'Reverse order
'DataList.Remove("D") 'Removes an element
For Each strItem in DataList
Wscript.Echo strItem
Next
'*******************************************************************************
'Random number between 1 and 100 (1 included, 100 aparently not included)
Set objRandom = CreateObject("System.Random")
Wscript.Echo objRandom.Next_2(1,100)
'*******************************************************************************
'Concatenate strings. Faster than using "&"
set wrt = CreateObject("System.IO.StringWriter")
for i = 1 to 100000
s = CStr(i)
wrt.Write_12 s 'String. Write_11 for decimal, _10 for double, _9 for float
next
s = wrt.GetStringBuilder().ToString()
'*******************************************************************************
set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(1, "loneliest")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(2, "happiest")
WScript.Echo sb.ToString()
'Prints:
'1 is a loneliest number
'2 is a happiest number
'*******************************************************************************
Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."
s.Append_3 "and the rest."
wscript.echo s.Length
wscript.echo s.Capacity
wscript.echo chr(s.chars(0)) 'returns first char
wscript.echo s.Replace("t", "d").Replace("l", "k").toString
s.Insert_2 7, "insert this " 'after char 7, starting from 1
WScript.Echo s.tostring()
WScript.Echo s.remove(10,4).tostring() 'Removes 4 chars starting from position 10
WScript.Echo s.tostring()
s.clear 'Removes all characters from the stringbuilder instance
'*******************************************************************************
'A sorted dictionary object
'http://www.reliance-scada.com/en/support/articles/technical/vbscript-tip-working-with-an-object-list
Dim Message, SortedList, i, s
' Initializes the message.
Message = ""
' Creates a list.
Set SortedList = CreateObject("System.Collections.SortedList")
' Fills the list.
SortedList.Add "Point", 1
SortedList.Add "Point Cloud", 2
SortedList.Add "Curve", 4
SortedList.Add "Surface", 8
SortedList.Add "Polysurface", 16
SortedList.Add "Mesh", 32
' Returns the element count.
Message = Message & "1: " & SortedList.Count & vbCrLf
' Returns the value by key.
Message = Message & "2: " & SortedList("Surface") & vbCrLf
' Returns the value at the index.
s = ""
For i = 0 To SortedList.Count - 1
s = s & CStr(SortedList.GetByIndex(i)) & " "
Next
Message = Message & "3: " & s & vbCrLf
' Verifies the existence of the key.
Message = Message & "4: " & SortedList.ContainsKey("Polysurface") & vbCrLf
' Verifies the existence of the value.
Message = Message & "5: " & SortedList.ContainsValue(16) & vbCrLf
' Returns the index of the key (searches for the element by key).
Message = Message & "6: " & SortedList.IndexOfKey("Polysurface") & vbCrLf
' Returns the index of the value (searches for the element by value).
Message = Message & "7: " & SortedList.IndexOfValue(16) & vbCrLf
' Removes the element by key.
SortedList.Remove "Polysurface" & vbCrLf
' Removes the element at the specified index.
SortedList.RemoveAt 0
' Removes all elements.
SortedList.Clear
' Releases the list.
Set SortedList = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
'*******************************************************************************
'LIFO Last In First Out stack
Dim Message, Stack, Item, s
' Initializes the message.
Message = ""
' Creates a list.
Set Stack = CreateObject("System.Collections.Stack")
' Fills the list.
Stack.Push "Item_1"
Stack.Push "Item_2"
Stack.Push "Item_3"
Stack.Push "Item_4"
' Goes through the list elements.
s = ""
For Each Item In Stack
s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf
' Converts the list into an array.
Message = Message & "2: " & Join(Stack.ToArray, ",") & vbCrLf
' Returns the element count.
Message = Message & "3: " & Stack.Count & vbCrLf
' Verifies the existence of the value.
Message = Message & "4: " & Stack.Contains("Item_2") & vbCrLf
' Removes and returns the last element.
Message = Message & "5: " & Stack.Pop & vbCrLf
' Returns the last element (without removing it).
Message = Message & "6: " & Stack.Peek & vbCrLf
' Empties the list.
Stack.Clear
' Releases the list.
Set Stack = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
'*******************************************************************************
'FIFO first in first out queue
Dim Message, Queue, Item, s
' Initializes the message.
Message = ""
' Creates a list.
Set Queue = CreateObject("System.Collections.Queue")
' Fills the list.
Queue.Enqueue "Item_1"
Queue.Enqueue "Item_2"
Queue.Enqueue "Item_3"
Queue.Enqueue "Item_4"
' Goes through the list elements.
s = ""
For Each Item In Queue
s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf
' Converts the list into an array.
Message = Message & "2: " & Join(Queue.ToArray, ",") & vbCrLf
' Returns the element count.
Message = Message & "3: " & Queue.Count & vbCrLf
' Verifies the existence of the value.
Message = Message & "4: " & Queue.Contains("Item_2") & vbCrLf
' Removes and returns the first element.
Message = Message & "5: " & Queue.Dequeue & vbCrLf
' Returns the first element (without removing it).
Message = Message & "6: " & Queue.Peek & vbCrLf
' Empties the list.
Queue.Clear
' Releases the list.
Set Queue = Nothing
' Displays the result.
MsgBox Message, vbSystemModal
--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--