Dynamic Goto

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
 
D

Dirk Goldgar

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
 
M

Marshall Barton

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
 
J

Jack Leach

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top