Forum Discussion

hao_bai's avatar
hao_bai
Occasional Contributor
16 years ago

How can I convert Object to string and print the error warning message directly via VB?

How can I convert Object to string and print the error warning message directly via VB?

<code>

Sub ObjSearch(Object)  

  If Not Object.Exists Then

    Log.Message("Not found Object " + CStr(Object))

  End If    

End Sub



Sub Main()

  Call ObjSearch(Aliases.Sys.processCMD.CMDClass)                          

End Sub

</code>

Will get a "An exception occurred in the "Unit1" unit at line 5:VBScript don't support Cstr"


20 Replies


  • Hi Trung,


     


    Something like this:




    ...


    RunModel("F","D","U", Aliases.Sys.scenarioA.Toolbar)


    ...


    Sub RunModel(key1,key2,key3,ObjectName)


      Call Run3Key(key1,key2,key3)     


      If Not ObjectName.Exists Then


        Runner.Halt("Run " & ObjectName.MappedName & " Failed...")   


      End If


    End Sub



  • hantrung_truong's avatar
    hantrung_truong
    Occasional Contributor
    Thanks for your quick response Tanya. However, I still receive the error:



    An error occurred while accessing the "MappedName" method or property of the "pnl_Error" object.

    The object or one of its parent objects was not found.



    The problem is that the object does not exist, so the "MappedName" property is invalid.



    Do you have any other ideas about this?



    Thanks

  • Hi Trung,


     


    The MappedName property returns the mapped name of the object you are working with. It's used on the line where the error text is posted to the Test Log. You can use any other property (or even only a predefined string) to post the informative message. I suggest that you set a breakpoint on the problematic line and evaluate the object to identify what property you can use.


     

  • hantrung_truong's avatar
    hantrung_truong
    Occasional Contributor
    Thanks Tanya. Now I figure out the root cause is that I couldn't convert an object to a string. From my researches, I used CStr(object_name) to covert the object to string. Unfortunately, it doesn't work. Could you please give me advice on how to convert an object to a string.



    Thanks
  • Ravik's avatar
    Ravik
    Super Contributor
    Hi All, You can convert object value and type by using


    aqConvert.VarToStr


     aqConvert give the several method to convert object, based on your need you can convert it. please try and let us know you feedback. .... .. also If You get any Error of VB like Object Required, Object not found and any Exception you can by pass it by using ON Error Resume Next ..........Ravik
  • hantrung_truong's avatar
    hantrung_truong
    Occasional Contributor
    Thanks all.



    As I understand the problem deeper, it is resolved now. What I was facing is that I tried to convert a mapped name to a string using CStr and it is impossible.



    Example:



    '---Don't care if the object exists or not, just log the mapped aliases as a string

    '---I wanted to see a string "Aliases.pageLogin.txt_Username" in the log



    Log.Message(CStr(Aliases.pageLogin.txt_Username))



    '--Now, I have to make it as Tanya suggested, but this is only for the existed objects.



    Log.Message(Aliases.pageLogin.txt_Username.MappedName)



    Actually, it would be great if the script with CStr works.



    Thanks.





  • Hallo,



    I have a clarifying question about David's answer #7 in this thread.



    How/why does the Eval() function convert the string into an object reference?



    Reading the MSDN definition of this function, it sounds like Eval is a sort of interrogation function, not a conversion function.



    Thanks in advance!



    Daniel
  • Can someone at SmartBear answer my question from last week please?



    Thanks!
  • Wow, thanks a bunch Alexei!



    Is there documentation on this somewhere that I could read?



    In other words, how did you figure all that out?



    Thanks again.



    Daniel
  • AlexKaras's avatar
    AlexKaras
    Community Hero
    Hi Daniel,



    > In other words, how did you figure all that out?

    Mostly from the MSDN (and maybe a little bit from the further experience).

    Glad that my explanation helped...