Forum Discussion

kalldrexx's avatar
kalldrexx
Contributor
13 years ago

How can I convert my TC objects in VB.Net to a compatible var type

I have a C# connected application that controls TestComplete/TestExecute.  I am looking at hooking into my vast library of helper methods with a VB.Net class (so that less experienced QA developers can use it to write testing code).  



Right now in C# I have the following helper method:










public class CamaUtils

{

    /// <summary>

    /// Sets the text of a CustomCama text field

    /// </summary>

    /// <param name="textfield">Text field's test complete object</param>

    /// <param name="value">Value to set the text field to</param>

    /// <param name="useTabToExit">If Tab is used to exit the text field.  If false this function will NOT remove the cursor from the text field</param>

    public static void SetTextFieldValue(var textfield, string value, bool useTabToExit = true)

    {

        if (textfield == new var(null))

            throw new ArgumentNullException("Attempted to set the value of a text field without passing in the text field object");





        textfield["SetFocus"]();

        textfield["Keys"]("^[Home]!^[End]"); // Clear the current value

        textfield["Keys"](TestingUtils.EscapeTestCompleteInputString(value));





        if (useTabToExit)

            textfield["Keys"]("[Tab]");

    }

}






This works fine when I interact with it from C#, but when I try to use the following VB.Net code:





Dim window = Aliases.WinRDE.WaitAliasChild("fmPermitInsert", 1000)

CamaUtils.SetTextFieldValue(window.txtPermitNo, action.Attributes.Item(PERMIT_NUM_PARAM))





This code causes the following exception:



Unable to cast COM object of type 'System.__ComObject' to class type 'AutomatedQA.script.var'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.



What is required for me to cast a test complete object into a TestComplete var type?

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Matthew,



    What is required for me to cast a test complete object into a TestComplete var type?


    Add the AutomatedQA.script.dll assembly to your Visual Basic .NET project and use New var(test_object):



    Dim txtPermitNo As New var(window.txtPermitNo)

    CamaUtils.SetTextFieldValue(txtPermitNo, action.Attributes.Item(PERMIT_NUM_PARAM))



    Alternatively, you can change your C# method to accept object instead of var, and cast the object to var in the method code:



    public static void SetTextFieldValue(object textfieldobject, string value, bool useTabToExit = true)

    {

      var textfield = new var(textfieldobject);



    However, we strongly recommend using a single language for your Connected Applications to avoid interoperability issues.
  • Thanks,



    I went with the passing in object solution to make it easier for now.  As much as I'd like to force all the other QA people to learn and use C# I think that will take time away from the strained resources we already have.
  • Unfortunately, while this works for VB.Net objects, it does not work when you pass in C# var objects.  For example, take the following method:






            public static void SetTextFieldValue(object textfieldObject, string value, bool useTabToExit = true)

            {

                var textfield = new var(textfieldObject);

                // .... do stuff here 

             }






    if you pass in a var as the textfieldObject parameter, it fails with the (useless) exception of "var(VarDelegate)"
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Matthew,



    A complete code sample would be helpful to identify the issue.



    Also, please give me the following information:

    * the TestComplete version as described in Help > About,

    * the Windows version and its bitness,

    * the .NET Framework version(s) installed on your computer,

    * the Visual Studio version,

    * your connected application's platform (AnyCPU/x86/x64) and Target Framework.



    Thanks.