Passing a subroutine as an argument to another subroutine

Hello, I've written a series of subroutines to solve a system of equations and it basically looks like this

sub driver
  call solver(arguments)
end sub

sub solver(arguments)
    ...
    call fcn(arguments)
    ...
end sub

sub fcn(arguments)
   'equations in here
end sub

I'd like to be able to solve several different systems of equations at one time.  In Fortran, I can pass the fcn subroutine as an argument to the solver subroutine and therefore achieve this aim in the following manner:

'driver
  external fcn1, fcn2
  call solver(fcn1, other arguments)
  call solver(fcn2, other arguments)
end

subroutine solver(fcn, other arguments)
    external fcn
    ...
    call fcn(arguments)
    ...
end

subroutine fcn1(arguments)
   'equations in here
end

subroutine fcn2(arguments)
   'equations in here
end

Is there a way to do this in Visual Basic?  Or can you please suggest an alternative method?  
sumingooiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hi sumingooi,

It's hard to give you specific advice here because I do not know anything about your app, but you may want to
make sure to set the arguments for the succeeding subs to be ByRef.

Regards,

Patrick
0
Bob LambersonSoftware EngineerCommented:
sumingooi,

> 'driver
>   external fcn1, fcn2
>   call solver(fcn1, other arguments)
>   call solver(fcn2, other arguments)
> end

The way it is usually done in VB is ........]
driver       'Call is not required to call a function or sub

function driver()
    solver(flag for fcn1, other arguments)
    solver(flag for fcn2, other arguments)
end function


function solver(flag for which function, other arguments)
    if flag = fcn1 then
        fcn1(arguments)
    elseif flag = fcn2 then
        fcn2(arguments)
    endif
end sub

sum fcn1(arguments)
   'equations in here
end

sub fcn2(arguments)
   'equations in here
end

If you expect the suroutines to return values, you will need to make them functions rather than subs. If you are just operating on variables in each sub it won't matter.

Bob
0
Bob LambersonSoftware EngineerCommented:
BobLamberson,
> sum fcn1(arguments)
sorry should be sub fcn1(arguments)

Bob
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sumingooiAuthor Commented:
Thanks Bob, using your approach does work, but I'd like to make my solver subroutine more general and "portable".  I don't want to have to specify the names of the function subroutines in the solver.  I'd like to be able to run the solver to solve any function regardless of its name, and not have to edit the solver each time I change functions.  

Cheers, Su Ming
0
Bob LambersonSoftware EngineerCommented:
sumingooi,
The only way I know of the pass functions as parameters is using API in you vb code. Here is a reference that may help you.
http://www.allapi.net/vbtutor/tutmain.shtml

Bob
0
PSSUserCommented:
I think what you want is a VB6 function call CallByName:
http://www.devx.com/tips/Tip/15422
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.