Forum Discussion

jyothi_priya's avatar
jyothi_priya
Contributor
13 years ago
Solved

Goto in VBScript

Hi,



I want following kind of code in VBScript:



Function ClickControl(Object,Label)

    If Object.Exists Then

        Object.Click()

    Else

        Goto Label

    End If

End Function



Sub T1



    XXX = NameMapping.Sys.Process("XXX")

    Call ClickControl(XXX,L1)



L1: End Sub



Sub T2



    YYY = NameMapping.Sys.Process("YYY")

    Call ClickControl(YYY,L2)



L2: End Sub



I know we can return values from function and can use If conditions. But this won't help me as there are many click operations in one sub routine and using If conditions that many times will be very tidious and script wont look neat.

Can't I use Goto like above in VBScript?




  • Hi,



    There is no GoTo label statement in VBScript. The GoTo keyword is used only as part of the On Error statement for disabling error handling, as follows:





    To control the test execution flow, you'll need to use If..Then..Else, Select..Case and other flow control statements.

3 Replies

  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
    Goto is archaic. No one should be using GOTO since at least 20-30 years ago, when nothing else was available. Even more than that. In 1968, Edsger Dijkstra published a paper called "Goto statement considered harmful". The only valid use of Goto nowadays is with On Error.



    There is certainly another better way for you to deal with your problem. Your different labels all do the same thing, with different parameters. Just make one function that tries to click your object, if it doesn't exist then try the same object name on two different processes.



    Here is what I would do:



    Sub ClickControl(ByRef objControl, arrProcessList)

        If objControl.Exists Then

            objControl.Click

        Else

            For Each ProcessName In arrProcessList

                Set objProcess = Sys.WaitProcess(ProcessName)

                If objProcess.Exists Then

                    Set objFoundControl = objProcess.FindChild(Array("WndClass", "Name"), Array(objControl.WndClass, objControl.Name))

                    If objFoundControl.Exists Then

                        objFoundControl.Click

                        Exit For

                    End If

                End If

            Next

        End If

    End sub




    You can call it by supplying an object reference and an array of the processes you wish to try, in order. For example:



    ClickControl SomeMappedProcess.VBObject("txtName"), Array("XXX", "YYY", "ZZZ")



    This will try to click the original object first. If the object or process does not exist, it will try to find an object of the same class with the same name in processes "XXX", "YYY", "ZZZ" in order. It will click the first object it finds and then return.





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



    There is no GoTo label statement in VBScript. The GoTo keyword is used only as part of the On Error statement for disabling error handling, as follows:





    To control the test execution flow, you'll need to use If..Then..Else, Select..Case and other flow control statements.
  • Ravik's avatar
    Ravik
    Super Contributor
    Hi All,



    I am also looking solution for that can performed operation same us GoTo .



    If GoTO Not working in VBScript then which is the alternative are available.

    Please help us.



    Thanks

    Ravik