-1

I need to find out how to reference a Subroutine within another module by a reference. This is what I am trying to do:

Module Mod1

   sub_x(pass a reference to this module)

   Private Sub close_me()
       ' do something here
   End Sub

End Module

Module Mod2

    Public Sub sub_x(get the reference to the passed module)
      reference to passed module.close_me()
    End Sub

End Module

Sub_x will receive calls from several different modules. All of the calling modules will have a close_me() subroutine. So I need to know which module is calling sub_x so I know which one to close.

John
  • 1,310
  • 3
  • 32
  • 58
  • modules in vb are like `shared` functions in classes. There is no concept of an instance of a module or 'closing' a module. If you want that functionality, use classes without shared methods. – ps2goat May 04 '15 at 21:09
  • I'm not really closing the module. The close_me() function refers to something going on inside the module. – John May 04 '15 at 21:13
  • Just realize that this will not be thread safe unless you build it with some of the built-in techniques, such as `Sync` – ps2goat May 04 '15 at 21:16

1 Answers1

0

In Mod2:

Public Sub sub_x(ByVal closeModule As Action)
    closeModule()
End Sub

In Mod1:

Mod2.sub_x(AddressOf Mod1.close_me)
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks Joel, this looks like it should work, but it give me an error in Mod1 saying that Me is not valid within a Module. – John May 04 '15 at 21:25
  • Change it to the name of the module. – Joel Coehoorn May 04 '15 at 21:29
  • I can't use the module name either, but if I just leave it off, it seems to accept it without error. If I mouse over it, it shows the name of the module I am referencing. However, when I run it, it does not actually call the module. If I trace it out, and I show the value of closeModule in Mod2, it shows that it is indeed System.Action. – John May 04 '15 at 21:37
  • After further review, I see that removing the "Me" reference and leaving it blank gives me the value of - {Method = {Void close_this_window()}} System.Action. I think the "Void" needs to be the name of my module. So I need to figure out how to pass it without error. – John May 04 '15 at 21:42
  • In the end this actually worked. I needed to leave off the "me" and "mod1" names and just use the name of the subroutine. Originally I did not include the "ByVal" reference in my parameters and it was causing it not to work, but once I included it, it started working. Thanks for your help. – John May 04 '15 at 23:00