What is DotNetFactory?
DotNetFactory is an object. It enables you to create an instance of a .NET object, and access its methods and properties.
You can also use DotNetFactory to access the static methods and properties of a class that does not have an instance constructor, for example, System.Environment.
DotNetFactory is a part of Utility Object. You do not need to have .NET Add-in installed in order to use this.
Example 1 of DotNetFactory
We will start with a simple example as shown in the QTP Help.
Set var = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
var.Show
wait 5
var.Close
[See the screenshot below of the above code run]
In the above example we are using CreateInstance method of DotNetFactory object. We are using CreateInstance method to create an instance of a blank Windows form object, display the form on screen, and then close it after two seconds. Show and Close method simply show and close the form respectively.
We are using System.Windows.Forms namespace and Form class in the first line of the code above. A Form is a representation of any window displayed in your application. The Form class can be used to create standard, tool, borderless, and floating windows. You can also use the Form class to create modal windows such as a dialog box.
CreateInstance Syntax
DotNetFactory.CreateInstance (TypeName [,Assembly] [,args])
TypeName: The full name of the object type e.g. System.Windows.Forms.Form
Assembly: System.Windows.Forms (in System.Windows.Forms.dll) - An Assembly is a logical unit of code, Assembly physically exist as DLLs or EXEs, One assembly can contain one or more files. Source
Args: The required arguments, if any.
Example 2 of DotNetFactory
Set var = DotNetFactory.CreateInstance("System.Environment")
msgbox var.CurrentDirectory
[See the screenshot below of the above code run.]
Above we are creating an instance of System Environment object. In the second line we are using the CurrentDirectory property which gets or sets the fully qualified path of the current working directory.
Similarly you can use:
msgbox var.Username
msgbox var.OSVersion
msgbox var.MachineName etc.
Few Methods and Properties can be found here.
Example 3 of DotNetFactory
Set var = DotNetFactory.CreateInstance("System.IO.File", "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll")
x= var.readalltext ("c:\a.txt")
msgbox x
File Class of System.IO namespace provides static methods for the creation, copying, deletion, moving, and opening of files etc.
The above example should also work fine without the assembly path as below:
Set var = DotNetFactory.CreateInstance("System.IO.File")
x= var.readalltext ("c:\a.txt")
msgbox x
ReadAllText: Opens a text file, reads all lines of the file, and then closes the file. It returns a string containing all lines of the file.
Example 4 of DotNetFactory
Set var = DotNetFactory.CreateInstance("System.IO.File")
set x= var.opentext ("c:\a.txt")
Do
s=x.readline()
If (s="") Then
Exit do
End If
msgbox s
Loop
OpenText Method opens an existing UTF-8 encoded text file for reading. It returns a value of type StreamReader. ReadLine Method of StreamReader object reads a line of characters from the current stream and returns the data as a string.
Example 5 of DotNetFactory
Set my_stack = DotnetFactory.CreateInstance("System.Collections.Stack")
my_stack.push("one")
my_stack.push("two")
my_stack.push("three")
msgbox my_stack.count
msgbox my_stack.pop
Stack Class represents a simple last-in-first-out collection of objects.
Push inserts an object at the top of the Stack.
Pop removes and returns the object at the top of the Stack.
See all classes represented by System.Collections Namespace here.
Example 6 of DotNetFactory
Set my_array = DotnetFactory.CreateInstance("System.Collections.ArrayList")
my_array.add("Hello")
my_array.add("World")
my_array.add("!")
msgbox my_array.count
For i = 0 To my_array.Count - 1
a=my_array.Item(cint(i))
msgbox a
Next
ArrayList Class of System.Collections namespace implements the IList (Represents a non-generic collection of objects that can be individually accessed by index.) interface using an array whose size is dynamically increased as required.
You can use the CInt function to provide internationally aware conversions from any other data type to an Integer subtype.
More on ArrayList members here.
Example 7 of DotNetFactory
Set sb = DotNetFactory.CreateInstance( "System.Text.StringBuilder" )
sb.Append "sachin"
sb.Append "ii"
sb.Replace "i", "x"
msgbox sb.ToString
msgbox sb.Length
StringBuilder Class represents a mutable (Changeable) string of characters.
Append (String) Method of StringBuilder class appends a copy of the specified string to the end of this instance. It returns a reference to this instance after the append operation has completed.
Replace replaces all occurrences of a specified character or string in this instance with another specified character or string.
ToString Method of StringBuilder class Converts the value of this instance to a String. It returns a string whose value is the same as this instance.
Above ii is being appended to sachin to form sachinii and then Replace replaces all i's in sachinii to x.
Example 8 of DotNetFactory
Set dt = DotNetFactory.CreateInstance( "System.DateTime" )
msgbox dt.now
Set sb = DotNetFactory.CreateInstance( "System.DateTime" )
set dt= sb.now
msgbox dt.day
msgbox dt.month
msgbox dt.year
DateTime Structure of System Namespace represents an instant in time, typically expressed as a date and time of day.
Now property gets a DateTime object that is set to the current date and time on computer, expressed as the local time.
Day gets the day of the month represented by this instance.
Month gets the month component of the date represented by this instance.
Year gets the year component of the date represented by this instance.
Set object = DotNetFactory.CreateInstance( "System.DateTime", ,year, month, day, hour, minute, second, millisecond )
Set two = DotNetFactory.CreateInstance( "System.DateTime", ,1998, 9, 1, 16, 30, 22, 5 )
msgbox two.tostring()
Set object = DotNetFactory.CreateInstance( "System.DateTime", ,year, month, day, hour, minute, second)
Set tme = DotNetFactory.CreateInstance( "System.DateTime", ,1980, 8, 15, 21, 30, 9)
msgbox tme.tostring()
More on DateTime members here.
Example 9 of DotNetFactory
format = "{0} Current Date & Time is {1}"
Set TDT = DotNetFactory.CreateInstance( "System.DateTime" )
Set SB = DotNetFactory.CreateInstance( "System.Text.StringBuilder" )
SB.AppendFormat format, "Hello,", TDT.now
Msgbox SB.ToString()
format is a variable. AppendFormat appends - Hello (in the {0}th place) and TDT.Now which is current date & time (in the {1}st place) - to the string contained in format.