none
Is "delegate" a statement? RRS feed

  • General discussion

  • See Delegate Statement. It clearly calls delegates statements. It also says a "delegate is a reference type" and "delegate class". I think it is confusing and not accurate to call the delegate type a statement.

    Note that that page is in the Statements section. When I go to Keywords (Visual Basic) "Delegate" goes to that page. I think that delegate (C# Reference) (for C#) is in the right place. Look at the difference in how they (delegates) are described yet they are the same thing. Note that the "Parts" in the Delegate "Statement" are essentially a subset of Function Statement (Visual Basic). The C# version is much smaller than the VB.Net version. Is it better to be shorter about this or has the C# version left out important information? In this case the C# version seems to me to be smaller because the extra stuff in the VB.Net page is unnecessary.

    There are some people that just automatically call everything keywords instead of researching what it is. Unfortunately that can cause confusion. It is especially important for developers to communicate using a common terminology whenever possible.

    What brought me to this is that I wanted to see the documentation of the "Invoke" member of the delegate class but it is quite difficult to find the documentation of the delegate class. I tried a few things to find it. Finally (while writing this) I thought of looking at the System namespace and that is where I found Delegate Class (System). It describes the Invoke method in a special note and implies we don't need to know about it.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 27, 2018 7:41 AM

All replies


  • There are some people that just automatically call everything keywords instead of researching what it is. Unfortunately that can cause confusion. It is especially important for developers to communicate using a common terminology whenever possible.


    Sam,

    Did you investigate the differences between the use of delegates in VB and C#. It is one of the main language differences. Where C# never got the advanced way of VB of using delegates and event handling. Many C# programmers who did not know that give endless advices to VB programmers how to use delegates. 

    The delegate is needed because it is not fully done automatic on invoking a control in a multi threading situation. But then we have probably had all where they are needed in VB.

    You are number nx telling this and Internet is full of that written by C# programmers. I had that experience long ago when a Fortran programmer showed how much better the sort could be done with Fortran than with Cobol. 

    He did not know a sort was one statement in Cobol. Then some persons tell it is better to know what happen while they themselves than drive a car with a full automatic gearbox which don't even have a key.

    If you use a high level program language, you don't need to know everything what happens and the time we wanted to spare a byte is very long ago. (The smallest item we can address in a 64bit program is 8 bytes) 

    Be aware that a delegate is just a kind of address in a way it was done already in 1955 (although it is now a virtual place)


    Success
    Cor








    Tuesday, February 27, 2018 8:04 AM
  • Did you investigate the differences between the use of delegates in VB and C#. It is one of the main language differences. Where C# never got the advanced way of VB of using delegates and event handling. Many C# programmers who did not know that give endless advices to VB programmers how to use delegates. 

    Delegates are a .Net Class but if you think that delegate in VB.Net is not the same thing than a .Net Class then please explain or at least provide a link to something authoritive written by someone that understands both VB.Net and C#. I think that many VB.Net programmers don't understand delegates and if I am correct then that documentation I found just makes things worse.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 27, 2018 8:35 AM
  • Did you investigate the differences between the use of delegates in VB and C#. It is one of the main language differences. Where C# never got the advanced way of VB of using delegates and event handling. Many C# programmers who did not know that give endless advices to VB programmers how to use delegates. 

    Delegates are a .Net Class but if you think that delegate in VB.Net is not the same thing than a .Net Class then please explain or at least provide a link to something authoritive written by someone that understands both VB.Net and C#. I think that many VB.Net programmers don't understand delegates and if I am correct then that documentation I found just makes things worse.


    Sam,

    It is impossible to find something about it on Internet because of the many C# developers who have tried to explain to VB developers what delegates are, while beside of that invoke you don't need it in VB.

    When Erik Meijer developed Linq in its current way (it was done wrong many times before). He found how easy it was in VB. 

    His main statement was. "I love VB". 

    The main reasons were the "Dim" keyword (the var in C#), the "With" keyword and the  build in way (lack off) of the Delegate. Can you show how to use delegates in Linq? Please explain it to me. 


    Success
    Cor


    Tuesday, February 27, 2018 8:53 AM
  • Delegates (language features/delegate/VB) are a concept which have a matching keyword.  A Keyword is exactly that, a single word which keys the complier on what actions to take.  A Statement is like a sentence and includes keywords plus other words that describe what the keywords will do.  So you have "delegates" as a concept, "Delegate" as a keyword, and a Delegate Statement written with the "Delegate" keyword.

    When I read all of those documentation pages, I read that they all say the same thing.  A delegate method points to some other existing method.  A delegate is a type-safe wrapper for another procedure.

    With the availability of the strongly typed delegate classes Action(Of T) and Func(Of T) (and their overloads) along with lambda methods, there is really no reason to ever write a Delegate statement in VB.

    Here's an example of writing a delegate statement the old way:

    Public Class Form1
        Delegate Function MathOperation(value1 As Double, value2 As Double) As Double
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim someMath As New MathOperation(AddressOf Add)
            Dim resultAdd = someMath(3, 2)
            Debug.Print(resultAdd) ' prints 5
    
            someMath = New MathOperation(AddressOf Subtract)
            Dim resultSub = someMath(3, 2)
            Debug.Print(resultSub) ' prints 1
        End Sub
    
        Private Function Add(value1 As Double, value2 As Double) As Double
            Return value1 + value2
        End Function
    
        Private Function Subtract(value1 As Double, value2 As Double) As Double
            Return value1 - value2
        End Function
    End Class


    And then here is a more modern way using Func(Of T) delegates instead of a custom delegate:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim someMath As Func(Of Double, Double, Double) = AddressOf Add
            Dim resultAdd = someMath(3, 2)
            Debug.Print(resultAdd) ' prints 5
    
            someMath = AddressOf Subtract
            Dim resultSub = someMath(3, 2)
            Debug.Print(resultSub) ' prints 1
        End Sub
    
        Private Function Add(value1 As Double, value2 As Double) As Double
            Return value1 + value2
        End Function
    
        Private Function Subtract(value1 As Double, value2 As Double) As Double
            Return value1 - value2
        End Function
    End Class

    By utilizing lambda methods we no longer need the explicitly defined Add and Subtract functions.  Here is the same example with lambdas:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim someMath As Func(Of Double, Double, Double) = Function(value1 As Double, value2 As Double)
                                                                  Return value1 + value2
                                                              End Function
            Dim resultAdd = someMath(3, 2)
            Debug.Print(resultAdd) ' prints 5
    
            someMath = Function(value1 As Double, value2 As Double)
                           Return value1 - value2
                       End Function
            Dim resultSub = someMath(3, 2)
            Debug.Print(resultSub) ' prints 1
        End Sub
    End Class

    In all of these examples "someMath" is a delegate variable.  In the first example, "MathOperation" is declared in a delegate statement.  The second two examples do not contain a Delegate Statement but still utilize delegates through an instance of the Func(Of T, T, T) generic delegate class.  The second is using object member methods and the third is using lambda methods.

    Does this help to explain why the documentation is correctly written?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, February 27, 2018 1:07 PM
    Moderator
  • Reed,

    I was afraid what your awaited response would be. 

    However, I like it. 

    :-)


    Success
    Cor

    Tuesday, February 27, 2018 1:14 PM
  • The keyword Delegate is a statement of declaration. I'm not really sure what else you would call it since it certainly is not an expression.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, February 27, 2018 1:31 PM
  • Can you show how to use delegates in Linq? Please explain it to me.

    We must first know what you need it to do.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 27, 2018 6:27 PM