Forum Discussion

Nimith's avatar
Nimith
Contributor
7 years ago
Solved

Can we return an object from a function

Hi,

 

Is there a way we can return an object to the calling function/sub from the called function? 

 

For Ex:

 

//Called function

Function Test1(btnName)
      Dim testObj
      If btnName = "CLOSE" Then
         Set testObj = Aliases.TMDS.WinFormsObject("frmBulletinHMI").FindChild("Name","WinFormsObject(""btnCloseTGBO"")", 5)
         Test1 testObj
     End If
End Function

 

//Calling sub

Sub Example
     Dim test
     Set test = test1("CLOSE")
     test.click
End Sub

 

 

While running this scripts, I get the error: 

    Object doesn't support this property or method 

 

 

 

Thanks,

Nimith

 

 

 

 

  • Got the solution! 

     

    You need to return as an object itself.

     

    In the called function: 

    Set Test1 = testObj    //while returning use the 'Set' keyword

     

    In the calling function:

    Set test = test1("CLOSE")

     

     

    Thanks!

2 Replies

  • Got the solution! 

     

    You need to return as an object itself.

     

    In the called function: 

    Set Test1 = testObj    //while returning use the 'Set' keyword

     

    In the calling function:

    Set test = test1("CLOSE")

     

     

    Thanks!

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      > Set Test1 = testObj В  В //while returning use the 'Set' keyword

      Small correction: Set keyword must always be used in VBScript when assigning an object to a variable regardless of whether this is just regular assignment or function's return value.