8

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?

3
  • You should consider looking at powershell. Commented May 14, 2012 at 11:50
  • 3
    String as the object you know from .NET is not available in VBScript. You would have to resort to the standard VBScript functions (len, lcase, ucase, etc). Commented May 14, 2012 at 12:31
  • 2
    Not possible. The COM interop built into the CLR will always convert it to a BSTR, the COM string type. The CreateObject call fails because System.String doesn't have a parameter-less constructor. Commented Aug 8, 2012 at 0:10

2 Answers 2

7

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.

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))
wscript.echo s.Replace("t", "d").Replace("l", "k").toString

gives

83
140
I
I kove deadkines. I kike dhe whooshing sound dhey make as dhey fky by.and dhe resd.

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

s.Insert 1, "insert this"

and

s.Insert_2 7, "insert this"

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

require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_get_methods
properties.each do |property|
  p property.to_s
end

gives a very long list like

"Application"
"Creator"
"Parent"
"ActiveCell"
"ActiveChart"
"ActiveDialog"
"ActiveMenuBar"
"ActivePrinter"
"ActiveSheet"
"ActiveWindow"
"ActiveWorkbook"
etc etc

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. And I do know why s.Insert(1, "insert this") is not working: Because you do not place a call statement in front of it, it is seen as a Sub and you can not call subs with parenthesis.
you're right, the call was left out with copying, the parenthesis is a "bad" habit from my javascript coding but what i wanted to say is s.Insert 1, "insert this" will never work because it's not exposed in the com while Insert_2 is and the challegne is how to find out what works and what not other then by experimenting and googling
I do use the .net system.collections a lot, and I do not know another way to get the correct number (or how do you call it) than just experimenting or googling it. Most of the time I create a wrapper class, so I can use easier method/function names (and introducing an extra layer of complexity :-\ ).
0

Have you tried. Msgbox len(s.ToString) or simply MsgBox(s.Length)

Alternatively you could create your own .Net class and expose it to com which would act as a wrapper for the static string functions you want to expose...

3 Comments

len(s.ToString) works of course, because len is a standard VBScript function, but that is not the point. s.Length gives me the length property of the StringBuilder and that returns 70, so that is correct, but I cannot use String methods, like s.ToLower or actually s.ToString.ToLower.
Why do you need those methods when there are native VBS functions that do the same thing just as easily. (Such as LCase)
@Nilpo, just to satifice my curiosity. Thereby, the string object has some methods that are not natively included in the VBS function set (formatting, padding, inserting, indexing). And I like the practice of method chaining, like myArray = myString.Trim().ToLower().Split(","), it is neater than myArray = Split(lCase(trim(myString)),",")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.