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?
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.
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
Re: What is different from a Delegate to a pointer?
Quote:
Originally Posted by
cicatrix
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.
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.
Re: What is different from a Delegate to a pointer?
I stand corrected what concerns ByVal and ByRef
Quote:
Originally Posted by
Shaggy Hiker
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).
Re: What is different from a Delegate to a pointer?
So more like a container for pointers?
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.
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?
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:
- 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:
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:
Private Function MyWork(ByVal id As MyDelegate) As String
' I do not know what will be done with these two integers, I simply invoke
' the delegate provided, get the return value and return it back
Dim PassedInt1 As Integer = 1
Dim PassedInt2 As Integer = 2
Dim ret As String = id.Invoke(PassedInt1, PassedInt2)
Return ret
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:
Private Function MyCallBack1(ByVal i1 As Integer, ByVal i2 As Integer) As String
Return (i1 + i2).ToString
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:
Private Function MyCallBack2(ByVal i1 As Integer, ByVal i2 As Integer) As String
' I can redefine the callback procedure
Return (i1 * i2).ToString
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:
Private Sub DoMyWork()
MsgBox(MyWork(AddressOf MyCallBack1))
MsgBox(MyWork(AddressOf MyCallBack2))
End Sub
Hope this helps. Sometimes using delegates is the only way out when you're in a multithreaded or distributed environment.
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
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.
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.
Re: What is different from a Delegate to a pointer?
Quote:
Originally Posted by
chris128
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.
Re: What is different from a Delegate to a pointer?
Quote:
Originally Posted by
Megalith
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)
Re: What is different from a Delegate to a pointer?
Quote:
Originally Posted by
Shaggy Hiker
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.
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.