delegates

Create empty C# event handlers automatically

It is not possible to fire an event in C# that has no handlers attached to it. So before each call it is necessary to check if the event is null. if ( MyEvent != null ) { MyEvent( param1, param2 ); } I would like to keep my code as clean as possible and get rid of those null checks. I don't think it will affect performance very much...

How to dynamically invoke a chain of delegates in VB.NET

Does someone knows if it's possible to dynamically create a call chain and invoke it? Lets say I have two classes A & B: public class A public function Func() as B return new B() end function end class public class B public function Name() as string return "a string"; end function end class I want to be a...

C# equivalent of C++ mem_fun?

I'd like to do something like the following in C#: class Container { //... public void ForEach(Action method) { foreach (MyClass myObj in sequence) myObj.method(); } } //... containerObj.ForEach(MyClass.Method); In C++ I would use something like std::mem_fun. How would I do it in C...

Using subclassing to replace a Java class that doesn't implement an interface

For example, java.io.File is just a concrete class. My replacement for it supports resolving Windows shortcuts. I need to preprocess constructor parameters to resolve possible .lnk files because the FileSystem object that does normalizing/canonicalision/resolving on the abstract paths is not accessible. The need for preprocessing rules o...

Creating a delegate type inside a method

I want to create a delegate type in C# inside a method for the purpose of creating Anonymous methods. For Example : public void MyMethod(){ delegate int Sum(int a, int b); Sum mySumImplementation=delegate (int a, int b) {return a+b;} Console.WriteLine(mySumImplementation(1,1).ToString()); } Unfortunately, I cannot do it i...

C# Events/Subscriptions .... listen to non-referenced projects

I am developing an application trying to employ the Observer pattern. Basically I have a base form from which various components (forms) can be loaded. The base form references each of the components and some of the components reference each other. If I want one of the components to listen for events raised by the base form (perhaps fr...

Why can C# do this and C++/CLI cannot?

.NET Framework 3.5 comes with all the LINQ goodies, and also includes predefined generic Func and Action delegates. They are generic for up to 4 arguments. I am writing a C++/CLI project that (unfortunately) uses VS 2005 and must only rely on the standard 2.0 assembly set (so no System.Core). I tried defining my own generic delegates (i...

C#: WebBrowser.Navigated Only Fires when I MessageBox.Show();

I have a WebBrowser control which is being instantiated dynamically from a background STA thread because the parent thread is a BackgroundWorker and has lots of other things to do. The problem is that the Navigated event never fires, unless I pop a MessageBox.Show() in the method that told it to .Navigate(). I shall explain: ThreadSt...

Uses of Action delegate in C#

I was working with the Action Delgates in C# in the hope of learning more about them and thinking where they might be useful. Has anybody used the Action Delgate, and if so why? or could you give some examples where it might be useful? ...

C# delegate for two methods with different parameters

I am using the following methods: public void M1(Int32 a) { // acquire MyMutex DoSomething(a); // release MyMutex } and public void M2(String s, String t) { // acquire MyMutex DoSomethingElse(s, t); // release MyMutex } From what I have found so far it seems that it is not possible to use a single delegate for two metho...

Event handling in Dynamic asp.net user Control

Hi all, I Have a page, where I want to dynamically add asp.net user controls. The scenario is that we want that on specific event of a control, It disposes itself and loads another control in the page. I am not able to have solution how to do this? any one have decent Idea, Please help.. Thanks Singh Ajay ...

Function pointers/delegates in Java?

Hello, for my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? ...

I still don't get Delegates

Hi, Isn't the use of delegates to help with some asynchronous cases? I tried the following but my UI still hangs. When on earth do you use delegates? Public Class Form1 Private Delegate Sub testDelegate() Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click ...

How do i check to see if a delegate is valid on the iPhone

Hiya, I'm doing something like this: @protocol CallbackDelegate -(void) performCallback; @end @interface MyObject : NSObject { id<CallbackDelegate> delegate; } -(void)AsyncFuncCall; @end @property (nonatomic, assign) id<CallbackDelegate> *delegate; The code that owns MyObject sets up a delegate to recieve the callback function...

Scope of variables in a delegate

I found the following rather strange. Then again, I have mostly used closures in dynamic languages which shouldn't be suspectable to the same "bug". The following makes the compiler unhappy: VoidFunction t = delegate { int i = 0; }; int i = 1; It says: A local variable named 'i' cannot be declared in this scope because it wou...

Using BeginInvoke/EndInvoke in a multithreaded fashion. How do AsyncCallback, AsyncWaitHandle and IsCompleted interact?

Andreas Huber's answer to this question gave me an idea to implement Concurrent<T> with async delegates instead of the ThreadPool. However, I am finding it harder to understand what's going on when an AsyncCallback is passed to BeginInvoke, especially when several threads have access to IAsyncResult. Unfortunately, this case doesn't seem...

C# 3.0 generic type inference - passing a delegate as a function parameter

I am wondering why the C# 3.0 compiler is unable to infer the type of a method when it is passed as a parameter to a generic function when it can implicitly create a delegate for the same method. Here is an example: using System; class Test { static void foo(int x) { } static void bar<T>(Action<T> f) { } static void test(...

Why must a lambda expression be cast when supplied as a plain Delegate parameter

Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string str = "woop"; Invoke(() => this.Text = str); // Error: Cannot convert lambda expression to type 'System.Delegate' // because it is not a delegate type Yet this works fine: string str = "woop"; Invoke((Action)(() => th...

Does DynamicInvoke on a remote object work asynchronously?

I have inherited some majorly spaghetti code (combination C#/VB) that I'm trying to understand here. This appears to be a really weird situation where there are two successive calls to fire events to a remote object, which is being done by calling the DynamicInvoke method of a delegate, of the form: delegate1.DynamicInvoke(args1); // s...

Can I get the signature of a C# delegate by its type?

Is there a straightforward way using reflection to get at the parameter list for a delegate if you have its type information? For an example, if I declare a delegate type as follows delegate double FooDelegate (string param, bool condition); and later get the type information for that delegate type as follows Type delegateType = typ...