• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

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?  
0
sumingooi
Asked:
sumingooi
2 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now