Forum Discussion

mathare's avatar
mathare
Contributor
13 years ago

InvalidCastException accessing Text property

I have set up an alias for an Infragistics UltraTree in my TC project and am trying to access various properties of the tree in my script. I can cast the Name property to a string fine but when I try the same with the Text property I can an invalid cast exception and can't understand why. Can anyone help? Code snippet below:

var dataTree = Connect.Aliases["InputDataTree"];


Connect.Log["Message"]((string)dataTree["Name"]());


Connect.Log["Message"]((string)dataTree["Text"]());










The exception details are:

"Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. 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."











I had a thought while writing this post - is it to do with the properties TC supports for UltraTree controls? Those listed here: http://support.smartbear.com/viewarticle/29033/










If I use a var data type rather than casting to a string I don't get the exception. Are there any downsides to just using a var type in this case? Is there any way I could safely cast to a string?













6 Replies

  • Hi Mat,


    Could you please clarify what version of the Infragistics UltraTree control and TestComplete you are currently using?


    Thanks in advance.

    Ann

  • Ann,



    I'm using TestComplete 8.70.727.7 and the UltraTree has a ProductVersion of 4.20112.0.0. I could upgrade to TC 9 if that would help any...



    Thanks

    Mat

  • Hi Mat,


    Try to remove parentheses after the property name in your script code:


    ...


    Connect.Log["Message"]((string)dataTree["Name"]);

    Connect.Log["Message"]((string)dataTree["Text"]);


    If this does not help, could you please launch TestComplete with administrator permissions and check whether the problem occurs?


    Thanks in advance.

    Ann

  • Ann,



    If I do as suggested Visual Studio (I'm writing my code in VS as part of a connected application rather than natively in TC) tells me "Cannot cast expression of type 'AutomatedQA.script.var.VarDelegate' to type 'string'". On that basis starting TC as administrator won't make any difference will it? Or do you think getting my VS code to start TC as administrator may help??
  • Hi,


    Thanks for pointing out that you are working with a connected application. I didn't take this into account.


    I guess, the Text property in your connected application holds a value of the Object type. To obtain a string value from it, you need to refer to a certain property of this object. Try to use Text.OleValue in your script code.

    Try the following code:


    Connect.Log["Message"](dataTree["Text"]["OleValue"]());


    If this is not the property you actually need, inspect your application in the Object Browser to find out the needed property.


    I hope this helps.

    Ann

  • I've checked in the object browser and the Text property is of type System.String, which is why I am confused about the cast exception.

    Oh well, I can work with var types for now.

    Thanks for the help :)