In VBScript, you can use certain .net classes using COM automation. This comes in handy when you want to use dynamic arrays, lists, queues etc.
It would be nice if I could use strings as objects, so I could do all fancy string stuff with it, but whenever I pass a string from another object, it is seen by VBScript as a literal string and not as a string object:
Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."
' This gives me the literal string
MsgBox s.ToString
text = s.ToString
' But unfortunately this won't work
MsgBox s.ToString.Length
Set stringRef = s.ToString
Also creating a string as a COM object won’t work:
Set s = CreateObject("System.String") ' Nope.
Is there someone who did manage this, or is having other thoughts about it?
You can use some methods and properties, just not all of them.
The following works, but from the moment you use toString you have a vbscript variable which behaves as such.
gives
But eg the following doesn’t work, although it is a method of stringbuilder http://msdn.microsoft.com/en-us/library/system.text.stringbuilder_methods.aspx
don’t ask me why
and
does work
I also program in Ruby where you can use these objects also and there it is the same behavior. For some objects i can enumerate the properties or methods like eg for Excel
gives a very long list like
But not so for System.Text.Stringbuilder, i suppose it is due to the way the programmer exposes his methods and properties to the outside.
Sadly, i don’t think it is possible to directly use System.String in vbscript.