Multiple Source Files and Delegate/AddressOf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eBob.com

    Multiple Source Files and Delegate/AddressOf

    (Not that size matters, but ...) I have a program which is getting too big.
    So I'd like to split it into several source files. I know that I can create
    a ModuleX.vb source file which looks like this ...

    Module ModuleX
    Public Sub DoX()
    MsgBox("message from DoX")
    End Sub
    End Module

    .... and a similar ModuleY.vb source file for a DoY subroutine, and then use
    an "on button click" routine in my Form1 code which looks like this ...

    Private Sub btnGo_Click(ByV al sender As Object, ByVal e As System.EventArg s)
    Handles btnGo.Click
    DoX()
    DoY()
    End Sub

    .... and that works fine.

    BUT ... what I really want to do is to use the ModuleY.vb and ModuleY.vb
    source files as above and use something like the following in my Form1 code
    ....

    Public Class Form1
    Inherits System.Windows. Forms.Form

    Delegate Sub DoerRoutine()

    Structure OneSite
    Dim SiteName As String
    Dim DoerRoutineDele g As DoerRoutine ' I want DoerRoutineDele g to be
    the address of the DoX or DoY subroutine
    End Structure

    Dim Sites(1) As OneSite

    .... but the code which tries to initialize the Sites structure ...

    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
    Handles MyBase.Load
    Sites(0).SiteNa me = "Site X"
    Sites(0).DoerRo utineDeleg = AddressOf (DoX())
    End Sub

    .... causes Visual Studio to complain - it puts a squiggly line under "DoX"
    with the explanation "Expression does not product a value."

    Maybe I could stumble about trying various things and eventually get this to
    work. But Id like to understand what is going on. Why is the initial,
    straightforward version OK and the version using Delegate and AddressOf not
    OK.

    Thanks, Bob


  • Jonathan Boivin

    #2
    Re: Multiple Source Files and Delegate/AddressOf

    Hi,

    Within your OneSite structure, you should change the type of the DoerRoutine
    to Delegate.
    Then, when you assign your sub to this, you have to use :
    New EventHandler(Ad dressOf METHODNAME)

    Then, I use (MyForm.Invoke( MySavingMethod) ) to call the specific method.
    Here, MyForm is the place where you want the call to be handle (example a
    form in this case. This place has to Invokable.). MySavingMethod represents
    the delegate to be used (for you it would be the DoerRoutine delegate).

    Hope this help,
    Jonathan Boivin
    ---
    jonathanboivin@ cints.net | http://www.cints.net
    "eBob.com" <fakename@total lybogus.coma écrit dans le message de news:
    %23KET$yNiHHA.2 08@TK2MSFTNGP05 .phx.gbl...
    (Not that size matters, but ...) I have a program which is getting too
    big. So I'd like to split it into several source files. I know that I can
    create a ModuleX.vb source file which looks like this ...
    >
    Module ModuleX
    Public Sub DoX()
    MsgBox("message from DoX")
    End Sub
    End Module
    >
    ... and a similar ModuleY.vb source file for a DoY subroutine, and then
    use an "on button click" routine in my Form1 code which looks like this
    ...
    >
    Private Sub btnGo_Click(ByV al sender As Object, ByVal e As
    System.EventArg s) Handles btnGo.Click
    DoX()
    DoY()
    End Sub
    >
    ... and that works fine.
    >
    BUT ... what I really want to do is to use the ModuleY.vb and ModuleY.vb
    source files as above and use something like the following in my Form1
    code ...
    >
    Public Class Form1
    Inherits System.Windows. Forms.Form
    >
    Delegate Sub DoerRoutine()
    >
    Structure OneSite
    Dim SiteName As String
    Dim DoerRoutineDele g As DoerRoutine ' I want DoerRoutineDele g to
    be the address of the DoX or DoY subroutine
    End Structure
    >
    Dim Sites(1) As OneSite
    >
    ... but the code which tries to initialize the Sites structure ...
    >
    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Sites(0).SiteNa me = "Site X"
    Sites(0).DoerRo utineDeleg = AddressOf (DoX())
    End Sub
    >
    ... causes Visual Studio to complain - it puts a squiggly line under "DoX"
    with the explanation "Expression does not product a value."
    >
    Maybe I could stumble about trying various things and eventually get this
    to work. But Id like to understand what is going on. Why is the initial,
    straightforward version OK and the version using Delegate and AddressOf
    not OK.
    >
    Thanks, Bob
    >
    >

    Comment

    • Branco Medeiros

      #3
      Re: Multiple Source Files and Delegate/AddressOf

      eBob.com wrote:
      <snip>
      Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
      Handles MyBase.Load
      Sites(0).SiteNa me = "Site X"
      Sites(0).DoerRo utineDeleg = AddressOf (DoX())
      End Sub
      >
      ... causes Visual Studio to complain - it puts a squiggly line under "DoX"
      with the explanation "Expression does not product a value."
      <snip>

      The correct syntax is:

      Sites(0).DoerRo utineDeleg = AddressOf DoX

      (no parenthesis)

      HTH.

      Regards,

      Branco.

      Comment

      • eBob.com

        #4
        Re: Multiple Source Files and Delegate/AddressOf

        Thank you Branco. I feel sort of foolish now to have posted a question
        about a simple syntax error message. But even knowing now that the error is
        a syntax error, I find the error message, "Expression does not produce a
        value", not very helpful.

        Thanks again, Bob


        "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
        news:1177707464 .826122.25530@r 35g2000prh.goog legroups.com...
        eBob.com wrote:
        <snip>
        >Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
        >System.EventAr gs)
        >Handles MyBase.Load
        > Sites(0).SiteNa me = "Site X"
        > Sites(0).DoerRo utineDeleg = AddressOf (DoX())
        > End Sub
        >>
        >... causes Visual Studio to complain - it puts a squiggly line under
        >"DoX"
        >with the explanation "Expression does not product a value."
        <snip>
        >
        The correct syntax is:
        >
        Sites(0).DoerRo utineDeleg = AddressOf DoX
        >
        (no parenthesis)
        >
        HTH.
        >
        Regards,
        >
        Branco.
        >

        Comment

        Working...