Multiple Source Files and Delegate/AddressOf

E

eBob.com

(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(ByVal sender As Object, ByVal e As System.EventArgs)
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 DoerRoutineDeleg As DoerRoutine ' I want DoerRoutineDeleg 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(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Sites(0).SiteName = "Site X"
Sites(0).DoerRoutineDeleg = 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
 
J

Jonathan Boivin

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(AddressOf 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
 
B

Branco Medeiros

eBob.com wrote:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Sites(0).SiteName = "Site X"
Sites(0).DoerRoutineDeleg = 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).DoerRoutineDeleg = AddressOf DoX

(no parenthesis)

HTH.

Regards,

Branco.
 
E

eBob.com

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
 

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