Delegates And Multicast Delegates In .NET
Mar 27, 2009What are Delegates and Multicast Delegates in VB.NET? How do I use them? provide a simple example to illustrate the concept.
View 1 RepliesWhat are Delegates and Multicast Delegates in VB.NET? How do I use them? provide a simple example to illustrate the concept.
View 1 RepliesI have a class that I want to be serializable but contains a public instance of delegate that, apparently can't be serialized: [Code] Is there a way to make it serializable however?
View 4 RepliesConsider these two functions[code]...
My question is: what is the right way to accomplish this to resolve the ambiguity so that I can pass value or references types and get overload 2, but pass Functions and get overload 1? Can I constrain the types in some way to make this happen?
I've been tinkering with Delegates, and now Multicast delegates. Every time I invoke two delegates It only displays one of the two delegates per invocation.
[Code]...
[Code]....
So func is a function that accepts a string and return a string. In practice, internally, func also utilizez text1, text2. In fact, func can even be a member function with access the object that owns it (something like me or this operator).
And all that can be accomplished without changing the signature of func? How do the compiler do this?
I am struggling to get the syntax for the reflection call GetCustomAttributes ... at least I don't seem to be able to get hold of the custom attribute which I've decorated a particular method with. The complicating factor maybe that I have passed that method into an extension method as a generic delegate. (The other, more likely, complicating factor is that I'm not sufficiently familiar with reflection or what happens when you pass a method as a parameter like this!)
The code appears to run, and when I step it, the count returned from the GetCustomAttributes is 1. I can't work out how to get hold of that custom parameter
[Code]...
is there a way to create an interface of delegates? Sub dostuff(byval arg1 as dele1)works, problem is that i have 8 delegates, so i will have to give it 8 signatures. that isn't so bad, but the problem is that when there are various combinations of signatures it gets real real bad.
Sub dostuff(byval arg1 as [Delegate])works, but it allows me to accept delegates beyond the 8 that i wanted (it's like declarign arguments with base Object)could i be able to do this:
Sub dostuff(byval arg1 as Idele1to8)
Is it possible to create anonymous delegates in vb.net version 8 or earlier? If so, could someone provide an example of the syntax?
View 2 RepliesI have a VBNET app to scan my Active Directory using WMI calls and return some basic informaiton such as Dell Model name, current user logged on, Dell Service Tag etc my issue is the app becomes unresponsive whilst it scans and i have to wait for it to finish looping before i can work with it, in the past a do events used to suffice
I think Multithreading is the way i may need to go, whats the best approach? I tried to use Delegates but it doesnt seem to have made any difference
below is my for next loop code that calls the functions, the last section "Private Sub Button1_Click" down is the Delegate code i attempted
[Code]...
Maybe I am just not reading the MSDN documentation correctly, but given a function that takes in one string parameter and returns type T, how can this be specifed as a shared function using Func()?
MSDN says Func(Of In T1, Out TResult), but all of their examples use the same data type, i.e., Func(Of String, String). I want to do Func(Of In String, Out T), where T is arbitrary (but I can constrain it if necessary by a base class). I want it shared/static at the class level, yet the encapsulating class will itself not be a generic class. It seems in this specific scenario, it's impossible to do what I want because the compiler would have no way of knowing what Type T is at runtime.
So is it possible to do generics on delegates or anonymous lambda expressions in VB.net (not C#)?
I can come up with, if I understood the solution better, I could probably phrase a better subject line.[code]...
View 2 RepliesI am having trouble with getting a delegate to work properly.Here's my
[Code]...
What is the difference b/w Delegates and Events. I have little bit confusion. Where delegates are used, and when we have to used delegates. Can, you give some examples(both delebates and events) in vb.net.
View 6 RepliesI was going through tutorials and found the topic of Events and Delegates. However, I fail to understand why should this be required ? Instead of having to raise events, can't one just call the necessary procedure ?Sorry for posing this basic question. Any replies or even links to existing posts are welcome. [Also not sure if this question strictly falls under VB.NET category.]
View 4 RepliesHowever, I am wondering if there are any workarounds or plans for incorporating this feature into VB.NET in the future?What I'd like to do:
Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, optional ByRef BufferPosition As Integer = 0) As T
'Implementation of a func that matches the delegate'
Class A
Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
....
In the absence of specifying "optional" inside the actual delegate itself, it'd at least be nice to be able to do it in the function implementation only:
Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, ByRef BufferPosition As Integer) As T
'Implementation of a func that matches the delegate'
Class A
Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
....
At least this second way, the functions for the delegate will always have a value mapped to each parameter, although some may come from the function side and not the calling side.
is there a way to check how many delegates are attached to an event, let's saymybutton.Click has 10 handlers, 5 by using the Handles keyword and another 5 added by using the AddHandler method (well this is not the point, then point is that i do not know how many handlers there will be). and i would want to create another button that had all 10 handlers of mybutton.Click attached to its Click event as well.
View 6 RepliesI'm looking for a reference that can explain the concept and use of the DataReceived event and Delegates subject.The reference has to be pretty simple as I'm an "old dog attempting to learn a new trick". I'm not ignorant of things programming, but am ignorant of things dealing with serial communication in VB2005.I already have a couple of O'Rielly books ("VB2005 Cookbook", "VB2005 Desktop Reference") and I have a book that deals with serial communication from a Jan Axle-something. Neither O'Rielly books have any info, and the Axle-someting book's remotely related examples are too confusing for this slow learner.
View 6 RepliesI find myself calling functions from lambdas frequently as the provided delegate does not match or does not have sufficient parameters. It is irritating that I cannot do lambda on subroutines. Each time I want to do this I have to wrap my subroutine in a function which returns nothing. Not pretty, but it works.
Is there another way of doing this that makes this smoother/prettier? I have read that this whole lambda inadequacy will probably be fixed in VS2010/VB10 so my question is more out of curiosity.
[Code]...
I have a form with a DataGridView control. When i load the form, i fill the control with several records. Now sometimes it can be 1 to 30, but sometimes it could be 10000+ records.Now, what i'm trying to do is to grid.AutoResizeColumns
As you can imagine, when i have 30 records it goes like the Flash, but the more records, the longer it takes; obviously..So, for now i have a ugly label saying "WAIT PLEASE" and waitcursor=true; Yes, horrible, i know...
So i tried to do the following:I created a form called frmWAIT, and have a timer in it that shows 4 pictures at 250ms intervals, to make some kind of animation.In my Primary form, i added a BackgroundWorker, and in the DoWork Event, i tried to grid.AutoResizeColumns and it won't do it.
[Code]...
I'm trying to find the public delegates contained within a bas module in an assembly. I can identify the bas module, find nested types, methods, etc. but there seems to be no way to get nested delegates.
Note: 'GetNestedTypes' does not return delegates for bas modules.
Convert between VB, C#, C++, & Java [URL]
what are delegates?How are the lambda expressions related to the delegates?
reading the msdn about the delegates is really confusing me ;so i need a start off from here before moving on to the msdn.
Can we pass parameters to a function using delegates
Private Sub UpdateLabel(byval text as string)
If Me.Label1.InvokeRequired Then
Me.Label1.Invoke(New MethodInvoker(AddressOf UpdateLabel))
Else
Label1.Text = text
[Code]...
In MSDN documentation defination about lambda expression is "A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid ". Here validity of delegate is necessery only when we are passing lambda expression as an argument to a method.
[Code]...
I'm trying to learn how to work with Multithreading as well as Threading.Timers. So far I can only sum up my output as broken. I have read lots of information but none of it seems to be sinking in. I can't seem to find any information that is exactly relevant to what I want to do to make good sense of it.All I want to do is run two separate threads with a Threading.Timer on each. One will show the time elapsed and one will show the current time.
View 1 RepliesWhether i should use multi-threading or delegates in a service that i am writing. I have written a couple of services before (with multi-threading) but think that i could have done a better job of it. The service does not have a UI, it only needs to interact with a database and around 800 ip addresses. I just need advice on which way would be better in terms of efficiency, if for example i needed to send data to all 800 ip addresses at once.
View 2 RepliesIs it possible to translate the following C# code into VB.NET, using VB 9.0?
delegate Stream StreamOpenerDelegate(String name);
void Exec1()
{
WorkMethod( x => File.OpenRead(x));
[CODE]...
Can I do something like this?:
Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream
Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
''
End Sub
[CODE]...
I'm trying to write some documentation, but I don't know VB syntax. Often I use Reflector to translate it, but I'm not sure it's working in this case. I'm also not clear on where I would need line continuation characters. In VB9, it's not possible to have multi-line lambdas (or Sub lambdas, which I did not ask about). In VB9, all lambdas return a value, and must be a single expression. This changes in VB10. VB10 will allow the above syntax, but VB9 will not. In VB9, if the logic involves multiple code lines, it must not be a lambda; you must put it into a named Function and reference it explicitly.
Like this:
Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream
Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
''
End Sub
[CODE]...
I think I have read somewhere that delegates could be used in place of Select Case but I cannot find anymore that article. Could anyone confirm and provide some sample if this is true?I've only used delegates in threading and I have been heavily using Select Case and thought I could try using delegates instead.
View 3 RepliesI've been trying to do this for a while now.What I intend to do is that when I load a webpage in a class, i raise event to my form that the data is recieved.
[Code]...
i don't know what i said in this thread's topic is right or wrong.Since my perception about Delegates in .NET is that it is type safe .Delegate object will store the reference of only those functions whose signature matches with Delegate's signature .That i know from last 1 and a half year but never experimented .
[Code]...
I am using AddHandler to wire a function to a control's event that I dynamically create:
[Code]...
This code is generating a run-time exception stating: Unable to cast object of type 'MyEventHandlerDelegate' to type 'System.EventHandler' What am I not understanding about System.Delegate even though AddHandler takes as an argument of type "System.Delegate"? What Type does "EventHandler need to be to cast to a type that AddHandler can accept?