I am new to QTP, just started using it. I have written one class definition in some functional library and also created a test as under:
Class ExcelFileReader
Public default Function Init(pathToExcel)
Dim objFSO
Dim result
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(pathToExcel) Then
Rem File Found
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.open(pathToExcel)
Else
REM File not found
result = vbOk
While result <> vbCancel
result = Msgbox ("Unable to Locate the file", 5, "Error")
Wend
ExitAction(1)
End If
End Function
End Class
Test:
Dim objExcelReader : Set objExcelReader = New ExcelFileReader
objExcelReader.Init("D:\mytest.xlsx")
I have associated the functional library with the test but still I am getting an error at line number 2 in test stating class definition not found. Also if I copy complete code in the same file “test” then the things are working as intended.
Thanks in advance 🙂
Classes have local scope in your library. You have to construct them with a public function to make them publicly available:
And in your other library:
Protip: You can pass initialization parameters into your constructor function.
EDIT
On request: how to pass constructor parameters. Just add them to your constructor function:
In my implementation I have sometimes the same object, but that gets ‘configured’ by multiple contructor functions. In your case you could have a
new_ExcelFileReader, anew_CSVFileReaderand anew_TabDelimitedReaderall pointing to the same object but configured differently.Another way to fancy up your code is to return the object (with the
mekeyword) by the init function. This will result in code like this:With a constructor function you can use it by just returning the object and then calling the
Initfunction.