RaiseEvent and Delegates

Roey

Junior Contributor
Joined
Oct 10, 2002
Messages
238
Location
Canada
I have a windows form that calls a business object, which in turns searches through a list of items and then needs to display a modal form upon a certain type of item being found. This modal form then needs to return a value to the object so that it can continue processing.

So far I have approached the problem as below:

Code:
----- Form --------

Dim WithEvents objTest As New BUS.BusRequirements()
Dim decCost As Decimal

decCost = objTest.Process()

----- End Form -----

----- Class -------

Public Event RaiseScreen(ByVal intItemID As Integer)

Private Sub Process()
.....
processing of part list
Upon certain condition
RaiseEvent RaiseScreen(intItemID)
continue processing
......

----- End Class -----

----- Form ---------

Private Sub ModalForm(ByVal intItemID As Integer) Handles objTest.RaiseScreen

Launch New Modal Form
User Selects Item
****Need to Return Value to calling Object******

----- End Form ------


My problem is that vb.net (or my lack of knowledge) is restricting me from returning a value when using RaiseEvent and an event handler. I have been looking around on the web and found some info on using a non multicast delegate, but haven't a clue.


Any help would be greatly appreciatted.....
 
Non multicast delegates are easy and great to use. Taka a look at this code. If you need additional info let me know.

Visual Basic:
Public Class DelegClass

    Delegate Function dCallback(ByVal text As String) As Integer

    Private Callback As dCallback

    Public Sub SetDeleg(ByVal d As dCallback)
        Callback = d
    End Sub

    Public Sub testDeleg()
        Dim rv As Integer = Callback("test 123")
        MsgBox(rv)
    End Sub

End Class

'in a form

    Private dc As DelegClass

    Private Function fCallback(ByVal text As String) As Integer
        MsgBox(text)
        Return 1
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dc = New DelegClass
        dc.SetDeleg(AddressOf fCallback)
    End Sub


    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        dc.testDeleg()
    End Sub
 
Back
Top