Forum Discussion

fmendes's avatar
fmendes
Contributor
13 years ago

Create Object Instance

Is it possible to create an object instance of a class my application uses?



i.e. My app uses a control called Vendor.Controls.StarEventArgs, I want to create an instance of this class - something like



var sArgs = new AppDomain("MyApp").dotNet.Vendor_Controls.StarEventArgs();

  • Julia_K's avatar
    Julia_K
    SmartBear Alumni (Retired)

    Hello Luis,


    TestComplete allows calling routines and creating instance of classes declared in your .NET assemblies either by using the CLR Bridge, or by using the AppDomain method.


    For example, the following code uses the AppDomain method to access the Test namespace declared in the TestedApp application and creates an instance of the TestClass:

    var Obj = Sys.Process("TestedApp").AppDomain("TestedApp.exe").Test.TestClass.zctor();


    To learn more about creating instances of the class declared in your tested applications, please see the Calling Functions From .NET Assemblies Help topic.


    Please let us know if this information does not help or if you have any additional questions.

    Thank you.

  • Julia, thanks for the reply. I call methods, access variables....with no problems, the problem is creating an instance of the class itself. All the examples I found on the help pages was about creating classes, but not instances of existent classes that live on my application assembly, or any other .NET assembly for that matter. Is that a way to do that?
  • Julia_K's avatar
    Julia_K
    SmartBear Alumni (Retired)

    Luis,


    To create an instance of a class existing in your tested .NET application (or in any .NET assembly), you need to call the class constructor. The class constructor name is _ctor(), but in order to make that name compatible with VBScript, TestComplete transforms the constructor name to zctor().


    For example, the following code creates an instance of the CSalary class:

    var CSalaryInstance = Sys.Process("UserApp").AppDomain("UserApp.exe").dotNet.UserApp.CSalary.zctor();


    Please take a look at the note in the Calling Functions From .NET Assemblies Help topic - it contains information on class constructors. The topic also contains an example that demonstrates how to create an instance of the System.String class.

    Still, we will improve the TestComplete documentation to make the information on creating class instances and the example easier to find. Sorry for the inconveniences.


    Please let us know if you need anymore assistance.

    Thank you.

  • This forum is excellent, great support to all questions I have posted here. Thanks a lot all!!!