locked
Returning value from delegate RRS feed

  • Question

  • Hi

    I have a delegate as below. How can I return the value of variable i back into sub xyz?

    Sub xyz()
        abc(Sub()
              dim i = 10
            End Sub)
    End Sub

    Thanks

    Regards


    • Edited by Y a h y a Wednesday, May 27, 2020 7:32 PM
    Wednesday, May 27, 2020 7:31 PM

All replies

  • Maybe like this:

    Sub xyz( )

       Dim i as Integer

       abc(Sub( )

          i = 10

       End Sub)

       . . .

    End Sub

    This assumes that abc executes the received delegate.


    Wednesday, May 27, 2020 7:50 PM
  • I see a proper delegate as per

    Public Class Form1
        Private _value As Integer
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim operations As New Operations
            AddHandler operations.OnMonitor, AddressOf MonitorWork
            operations.Example1(12)
            MessageBox.Show(_value.ToString())
        End Sub
    
        Private Sub MonitorWork(args As MonitorArgs)
            _value = args.Value
        End Sub
    End Class
    ''' <summary>
    ''' Belongs in it's own file
    ''' </summary>
    Public Class Operations
        Public Event OnMonitor As DelegatesModule.MonitorHandler
        Public Sub Example1(upperValue As Integer)
    
            For index As Integer = 0 To upperValue
                OnMonitorEvent?.Invoke(New MonitorArgs(index + 5))
            Next
        End Sub
    End Class
    ''' <summary>
    ''' Belongs in it's own file
    ''' </summary>
    Public Class MonitorArgs
        Inherits EventArgs
        Public Sub New(value As Integer)
            Me.Value = value
        End Sub
        Public ReadOnly Property Value() As Integer
    End Class
    ''' <summary>
    ''' Belongs in it's own file
    ''' </summary>
    Public Class DelegatesModule
        Public Delegate Sub MonitorHandler(args As MonitorArgs)
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 27, 2020 8:41 PM