Dynamic Goto

  • Thread starter Thread starter QB
  • Start date Start date
Q

QB

I have a function in which I wish to use an input variable in conjunction
with Goto, but it generates an error. Can this done?


Function DoMyWorkForMe(sGoto as string)
Goto sGoto

Test:
Msgbox ""

End function

When I run DoMyWorkForMe("Test") and give me a label not defined ("Étiquette
non définie" - French PC). Is there a way to make this work or do I need to
change my basic concept for my function?

Thank you

QB
 
QB said:
I have a function in which I wish to use an input variable in conjunction
with Goto, but it generates an error. Can this done?


Function DoMyWorkForMe(sGoto as string)
Goto sGoto

Test:
Msgbox ""

End function

When I run DoMyWorkForMe("Test") and give me a label not defined
("Étiquette
non définie" - French PC). Is there a way to make this work or do I need
to
change my basic concept for my function?


Unfortunately, you have to change your basic concept, as the target of the
GoTo statement cannot be evaluated at run time. You could have a Select
Case statement:

Function DoMyWorkForMe(sDoWhat as string)

Select Case sDoWhat

Case "This"

' ... do "This" stuff ...


Case "That"

' ... do "That" stuff ...


Case "The Other"

' ... do "The Other" stuff ...

Case Else

MsgBox "What did you want to do?"

End Select

End Function

Or you could write a different public Function or Sub to perform each block
of work, pass the name of the desired procedure to function DoMyWorkForMe,
and use the Run method to call the procedure by name:

Function DoMyWorkForMe(sDoWhat as string)

Application.Run sDoWhat

End Function
 
QB said:
I have a function in which I wish to use an input variable in conjunction
with Goto, but it generates an error. Can this done?


Function DoMyWorkForMe(sGoto as string)
Goto sGoto

Test:
Msgbox ""

End function

When I run DoMyWorkForMe("Test") and give me a label not defined ("Étiquette
non définie" - French PC). Is there a way to make this work or do I need to
change my basic concept for my function?

I believe you need to forget about GoTo and use Select Case
instead.

Select Case sGoto
Case "Test"
'do something
Case "abc"
. . .
Case Else
'Bad argument value or you fogot to add the Case for it.
End Select
 
As insinuated, the Goto statement is a leftover from earlier, pre-oop
languages and should be avoided. It is used to set the error handler and the
beginning of a procedure and to redirect to an exit procedure, but should
otherwise be disregarded as a tool to use in code.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
Back
Top