Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get a .Net string object instance in VBScript?

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?

like image 791
AutomatedChaos Avatar asked May 14 '12 11:05

AutomatedChaos


People also ask

How do I find a character in a string in VBScript?

The InStr function returns the position of the first occurrence of one string within another. The InStr function can return the following values: If string1 is "" - InStr returns 0.

Is VBScript OOP?

VBScript's implementation of OOP doesn't support these two additional requirements, and is therefore not a true object-oriented programming language. As an ASP developer, you've already used object-oriented code written by others.

What is CreateObject in VBScript?

Creates an Automation object of the specified class. If the application is already running, CreateObject will create a new instance. This method is provided so that other applications can be automated from Microsoft Visual Basic Scripting Edition (VBScript) 1.0, which did not include a CreateObject method.


1 Answers

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.

like image 167
peter Avatar answered Sep 27 '22 22:09

peter