Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Function Delegate

Function Delegate

Scheduled Pinned Locked Moved Visual Basic
questiontutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nlarson11
    wrote on last edited by
    #1

    Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?

    Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form

    Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
    If oFrm Is Nothing Then Return Nothing
    If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
    Return oFrm
    End Function

    Thanks :confused: Nathan

    'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

    D J G 3 Replies Last reply
    0
    • N nlarson11

      Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?

      Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form

      Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
      If oFrm Is Nothing Then Return Nothing
      If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
      Return oFrm
      End Function

      Thanks :confused: Nathan

      'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      What do you mean by "it doesn't work"? Does it compile? throw an exception? Return an unexpected result? ...

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      N 1 Reply Last reply
      0
      • N nlarson11

        Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?

        Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form

        Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
        If oFrm Is Nothing Then Return Nothing
        If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
        Return oFrm
        End Function

        Thanks :confused: Nathan

        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

        J Offline
        J Offline
        John_Adams
        wrote on last edited by
        #3

        Hi Nathan, Following is a demo of Funtion Delegates in VB.Net BEGIN CODE Imports System Public Class MainClass Dim emp As New Employee("First Name", "Last Name") Private Delegate Function NumEmployeesDelegate() As Integer Shared Sub Main(ByVal args As String()) Dim show_num As NumEmployeesDelegate show_num = AddressOf Employee.GetNumEmployees Console.WriteLine(show_num().ToString) End Sub End Class Public Class Employee Private m_FirstName As String Private m_LastName As String Private Shared m_NumEmployees As Integer = 0 Public Shared Function GetNumEmployees() As Integer Return m_NumEmployees End Function Public Sub New(ByVal first_name As String, ByVal last_name As String) m_FirstName = first_name m_LastName = last_name m_NumEmployees += 1 End Sub Public Overrides Function ToString() As String Return m_FirstName & " " & m_LastName End Function Private disposedValue As Boolean = False ' To detect redundant calls End Class END CODE Hope thhis helps :)

        Regards, John Adams ComponentOne LLC

        N 1 Reply Last reply
        0
        • D Dave Kreskowiak

          What do you mean by "it doesn't work"? Does it compile? throw an exception? Return an unexpected result? ...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #4

          Sorry for leaving that out...it says I'm still in violation of cross-threading. The invoke should of got me on the forms thread but it is not working.

          'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

          G 1 Reply Last reply
          0
          • J John_Adams

            Hi Nathan, Following is a demo of Funtion Delegates in VB.Net BEGIN CODE Imports System Public Class MainClass Dim emp As New Employee("First Name", "Last Name") Private Delegate Function NumEmployeesDelegate() As Integer Shared Sub Main(ByVal args As String()) Dim show_num As NumEmployeesDelegate show_num = AddressOf Employee.GetNumEmployees Console.WriteLine(show_num().ToString) End Sub End Class Public Class Employee Private m_FirstName As String Private m_LastName As String Private Shared m_NumEmployees As Integer = 0 Public Shared Function GetNumEmployees() As Integer Return m_NumEmployees End Function Public Sub New(ByVal first_name As String, ByVal last_name As String) m_FirstName = first_name m_LastName = last_name m_NumEmployees += 1 End Sub Public Overrides Function ToString() As String Return m_FirstName & " " & m_LastName End Function Private disposedValue As Boolean = False ' To detect redundant calls End Class END CODE Hope thhis helps :)

            Regards, John Adams ComponentOne LLC

            N Offline
            N Offline
            nlarson11
            wrote on last edited by
            #5

            Hey John, Thank you. I need an example that deals with a Forms UI thread where I have to test if an invoke is required and invoke to get on that form's thread. I know i can do this with a Sub Delegate but the Function Delegate I can't?

            'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

            1 Reply Last reply
            0
            • N nlarson11

              Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?

              Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form

              Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
              If oFrm Is Nothing Then Return Nothing
              If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
              Return oFrm
              End Function

              Thanks :confused: Nathan

              'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

              G Offline
              G Offline
              Gideon Engelberth
              wrote on last edited by
              #6

              Is the error happening inside this function or after the function call when you use the value returned from it? If it is the latter, than the problem is that the call to Invoke only makes GetOnFormsUIThread be on the main thread. The function that called it originally is still on a different thread and any access to the members of the form returned from the function will continue to throw an exception.

              1 Reply Last reply
              0
              • N nlarson11

                Sorry for leaving that out...it says I'm still in violation of cross-threading. The invoke should of got me on the forms thread but it is not working.

                'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                nlarson11 wrote:

                The invoke should of got me on the forms thread

                The Invoke does execute in the UI thread, but that is totally pointless as all you are doing is returning a reference to the form. The reference doesn't get any different from being returned from another thread, and when you use the reference, you are still using it from your thread. It's whatever you do with the form that you need to call using Invoke.

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups