PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 What is different from a Delegate to a pointer?-VBForums
Results 1 to 17 of 17

Thread: What is different from a Delegate to a pointer?

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    What is different from a Delegate to a pointer?

    Hi All,

    I have been looking into delegates today, have come across them a few times but haven't understood them. I started thinking today that a Delegate is like a Pointer (for those who don't know a pointer is a reference to a location used in C++ and other languages than VB) So how much of a pointer is a delegate? what can a delegate do that a pointer/pointers cant?

    Similarly does anyone else agree with me that all .NET languages should be able to do the same things, why cant i use pointers or logical shift operators in VB.NET? Would you guys think that being able to use code from any .net language within your code would be advantageous?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    Delegate is not a pointer by itself, but it describes a pointer.
    Delegate is just a way to describe the method's arguments and its return types (this in common is called method's signature).

    So, in order to invoke a method you not only need to know its address, but also the arguments it accepts and the return type. That's why you need a delegate.

    Basically, a delegate is a type that describes a method.

    So, to your second question. If you look closely at .NET Framework you'd understand that virtually everything IS a pointer here. (We call it reference though). There are two kinds of types though - reference types and value types, but this division is artificial. For example you can use IntPtr type instead of Int32 to work with pointers.

    Exactly because of the fact that we work with pointers (references) everywhere, the default keyword for passing arguments is ByVal, not ByRef - to protect data. With ByVal you pass a copy of your object, but with ByRef you pass a reference (a pointer) to your original object.

    And finally, there ARE left shift (<<) and right shift (>>) operators in vb.net

    Basically, when working with .NET Framework the choice of language is just a matter of syntax preferrence. If you are used to C-like syntax you can use C#, if you have a VB background - use VB.Net. They're completely interchangeable.

    This is how CLR works: Everything that can be made in C# can be made in VB also and vice versa.

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: What is different from a Delegate to a pointer?

    Hi Cicatrix, thanks for the reply. I didn't know about the shift operations in VB.NEt, will look into that. There are non the less language specific differences, xml literals are only available in VB.NET for instance. when i started with .NET i believed that you could do anything in whatever flavour you chose but i now know that there are small subtle differences between languages and there implementation. I expect that as more revisions of the NET framework come out that the difference will entirely be down to syntax and see this as a good thing.

    I Notice that Delegates only work within the scope of the call unlike a pointer which can access anything (which obviously can be extremely bad for your code if you don't know what you are doing) Delegates therefore are a lot safer than pointers.

    I'm glad however that i have grasped Delegates to some extent, now i just need to think of examples where i need to use them lol
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,534

    Re: What is different from a Delegate to a pointer?

    Quote Originally Posted by cicatrix View Post
    With ByVal you pass a copy of your object, but with ByRef you pass a reference (a pointer) to your original object.
    That's not quite true. If the argument is a reference type and you pass it ByVal, you are passing a copy of the pointer, not a copy of the object. If you pass a reference type ByRef, you are passing a pointer to the pointer. If you pass a value type ByVal, then you are actually passing a copy of the value, whereas if you pass a value type ByRef, then you are passing a pointer to the object, which may well mean that your value type gets boxed (packaged into an object).

    For several years, the C# and VB groups within MS worked independently, which allowed them to diverge in features. A couple years back, MS changed that so that the two teams work together with the intention that the feature set of the two would converge back to a common set. That convergence wasn't all that easy, though, so as far as I know, it hasn't fully happened, yet.

    As for delegates, under the hood I expect that they are pointers to functions.
    My usual boring signature: Nothing

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    I find VB's syntax a bit redundant but it helps novices to understand the code better.
    The only thing I miss from C# is regions that allow me to hide some blocks of code within a particularly long sub or function.

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    I stand corrected what concerns ByVal and ByRef

    Quote Originally Posted by Shaggy Hiker View Post
    As for delegates, under the hood I expect that they are pointers to functions.
    No, they are not.
    They are TYPES, same as Integer or String.
    But they are Types of Pointers to functions. (A pointer to function is returned by the AddressOf operator).

  7. #7

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: What is different from a Delegate to a pointer?

    So more like a container for pointers?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,534

    Re: What is different from a Delegate to a pointer?

    I see what you are saying, and I can't say that I have used delegates enough to come up with any good conceptual model of them. I always figured that they wrapped a function pointer just because function pointers are useful and are not otherwise directly supported in the language.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: What is different from a Delegate to a pointer?

    so what about some good examples of where one would need a delegate? I was reading while looking into this that events and handles are delegates, is this correct?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    Let me explain.
    When you define a type Integer you say that you will need at least 4 bytes to store its value. By itself Integer is just a type - you cannot store actual values in it.

    But when you declare a variable:
    vb.net Code:
    1. Dim i As Integer
    - you say that you need a place in your app's memory heap to store the values for this variable.

    the same with delegates
    You declare that there is somewhere a method that accepts two integers and returns a string.
    So you declare a delegate:
    vb.net Code:
    1. Delegate Function MyDelegate(ByVal Arg1 As Integer, ByVal Arg2 As Integer) As String

    The part (ByVal Arg1 As Integer, ByVal Arg2 As Integer) As String is called a method's signature and it says that when you need to invoke something using this delegate you will have to provide exactly two integers and expect a string for return value.

    So now, I'm designing some class that uses a callback procedure. The user has to know what is the signature for that callback, so you declare the member like this:

    vb.net Code:
    1. Private Function MyWork(ByVal id As MyDelegate) As String
    2.         ' I do not know what will be done with these two integers, I simply invoke
    3.         ' the delegate provided, get the return value and return it back
    4.         Dim PassedInt1 As Integer = 1
    5.         Dim PassedInt2 As Integer = 2
    6.         Dim ret As String = id.Invoke(PassedInt1, PassedInt2)
    7.         Return ret
    8. End Function

    This can be helpful if you are working with third party components and you will have to redefine the behaviour of the algorithm from time to time.

    Of course we need a callback procedure that will be invoked by the MyWork, note that its 'signature' corresponds with the delegate signature:
    vb.net Code:
    1. Private Function MyCallBack1(ByVal i1 As Integer, ByVal i2 As Integer) As String
    2.         Return (i1 + i2).ToString
    3. End Function


    But what if we wanted to redefine the way our integers will be processed? Well, we provide another callback procedure for that. Note again that our second callback procedure has the identical 'signature':

    vb.net Code:
    1. Private Function MyCallBack2(ByVal i1 As Integer, ByVal i2 As Integer) As String
    2.         ' I can redefine the callback procedure
    3.         Return (i1 * i2).ToString
    4. End Function


    I call the MyWork function (it can be done even from a remote machine) and provide it with two pointers of the type MyDelegate:
    vb.net Code:
    1. Private Sub DoMyWork()
    2.         MsgBox(MyWork(AddressOf MyCallBack1))
    3.         MsgBox(MyWork(AddressOf MyCallBack2))
    4. End Sub

    Hope this helps. Sometimes using delegates is the only way out when you're in a multithreaded or distributed environment.

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: What is different from a Delegate to a pointer?

    The only place I find myself using Delegates is when working with multiple threads as cicatrix mentioned - basically you need to use them to be able to pass information from one thread to another. Take a look at JMC's codebank article here that describes how to get information from a background thread onto the UI thread (which is required to be able to update controls on the UI thread): http://www.vbforums.com/showthread.php?t=49838
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    Well, you will need delegates if you design a usercontrol, for example and need to implement a functionality that would allow a user to draw their own elements (like you draw custom listbox elements or menu items if you set OwnerDraw property to true).

    Basically, a user provides you with an address of a procedure you should invoke instead of performing the drawing within your class. Though, we can use events with the same efficiency.

    But then again, there are no events, under the hood, when raising an event your class actually invokes a handler procedure through delegate.

  13. #13

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: What is different from a Delegate to a pointer?

    Oh cool so say i had a multithreaded app and the secondary thread had a counter in it then i would use a delegate to get this information back to say populate a statusbar with, yeah can see the use in a delegate in that situation.

    Probably not a good example though as the secondary thread could be updating the statusbar :s I can see the point however if this secondary thread was a dll or seperate assembly within the project.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,534

    Re: What is different from a Delegate to a pointer?

    Quote Originally Posted by chris128 View Post
    The only place I find myself using Delegates is when working with multiple threads as cicatrix mentioned - basically you need to use them to be able to pass information from one thread to another. Take a look at JMC's codebank article here that describes how to get information from a background thread onto the UI thread (which is required to be able to update controls on the UI thread): http://www.vbforums.com/showthread.php?t=49838
    You actually don't need a delagate there, either. I use a different technique that I find more intuitive for making calls from one thread to another. Take a look at how I am using the SynchronizationContext in post #7 in this thread:

    http://www.vbforums.com/showthread.p...&highlight=UDP

    I save the context while in the UI thread, then post calls to the context from any other thread. In that example, I am posting a call to a method that raises an event, thereby raising the event on the UI thread, but you could use it for other types of calls, as well.
    My usual boring signature: Nothing

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: What is different from a Delegate to a pointer?

    Quote Originally Posted by Megalith View Post
    Probably not a good example though as the secondary thread could be updating the statusbar :s I can see the point however if this secondary thread was a dll or seperate assembly within the project.
    No the second through could not be updating the status bar directly because it would throw an exception (as that thread is not the thread that the control was created on)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: What is different from a Delegate to a pointer?

    Quote Originally Posted by Shaggy Hiker View Post
    You actually don't need a delagate there, either. I use a different technique that I find more intuitive for making calls from one thread to another. Take a look at how I am using the SynchronizationContext in post #7 in this thread:

    http://www.vbforums.com/showthread.p...&highlight=UDP

    I save the context while in the UI thread, then post calls to the context from any other thread. In that example, I am posting a call to a method that raises an event, thereby raising the event on the UI thread, but you could use it for other types of calls, as well.
    Yeah that is one way of doing it, although it seems a much less common way than just using delegates and I dont think its usable in every multi-threaded situation.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,534

    Re: What is different from a Delegate to a pointer?

    I wouldn't use it for direct manipulation of controls on a UI element, but for raising events, I find it more intuitive.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width