views:

271

answers:

2

I've been exploring options for expanding my QuickTest Professional scripting capabilities, and came across this article this morning, so I decided to experiment a bit. The code below works fine when executed inside the QTP environment, but I could see a use for this outside of the QTP environment as well. Unfortunately, it is causing an error when run from a stand-alone .vbs file

Set MyDate = DotNetFactory.CreateInstance("System.DateTime").Now
msgbox MyDate.ToShortDateString()

The error is "Object Required: 'DotNetFactory'"

I've made .Net calls from VBScript before, but none of them have used DotNetFactory. Code such as

Set coll = CreateObject("System.Collections.Queue")

Does not cause an error.

Do I need to set a reference to DotNetFactory? The text from the article

We use ‘System.DateTime’ as type name. We do not need to specify the assembly for this as it belongs to the already loaded namespace ‘System’ (mscorlib.dll).

makes me think so, because nothing is automatically loaded by my script editor. If so, how do I do this? I am not using an IDE, just a text editor, so any references would have to be loaded by the script itself.

Update: As pointed out by Motto, it can't be done without some extra work. The quote from the article was pointing out that System.DateTimenot DotNetFactory is included in mscorlib.

A: 

Can you create a DotNetFactory, as in :

Set dnf = CreateObject("Qualified.Name.To.DotNetFactory")
dnf.CreateInstance("System.DateTime")
Mikeb
@Mikeb - Since DotNetFactory lives inside of mscorlib.dll, how do I find its qualified name? I tried copying mscorlib.dll into the same directory as the script, just in case it was a pathing issue, but the same error still occurs. Searching MSDN and Google yields no usable results about DotNetFactory.
ssakl
+2  A: 

AFAIK DotNetFactory is an object created by QTP, not part of mscorelib as you said in a comment to Mikeb's answer. Therefore you can't access it from a stand along VBS file unless QTP has exposed a prog-id.

Motti
@Motti - Yes, it unfortunately appears that is the case. I believe the article was speaking about System.DateTime living in mscorlib, not DotNetFactory. Given that, is there a way to create an instance of System.DateTime via pure VBScript? CreateObject is causing errors for System.DateTime, but works for System.Collections.ArrayList without a problem. I don't have much .NET experience, so this has me a bit frustrated.
ssakl
It depends if the .NET class is COM visible, otherwise you can't create them directly from native (e.g. VBS) code. You can write your own .NET class that is COM visible and have it create types via reflection.
Motti
@Motti - Thanks for the tip. For my purposes, I think I can just stick to VBScript's built-in date/time functions.
ssakl