delegates

Adding one event handler to another

I have a class which wraps another class and exposes several events from the class it's wrapping. (The instance it wraps can change) I used the following code: public event EventHandler AnEvent; public OtherClass Inner { get { /* ... */ } set { //... if(value != null) value.AnEvent += AnEvent; ...

Simple Delegate Example?

Ok, I'm programming in objective-C and using Xcode. I have read through the documentation on Apple's website and understand what delegate's are but when I come to the part where it talks about how to actually implement delegate methods into code, I just become confused, especially when they say something like "now implement the delegate'...

How do I use eventhandler from baseclass

Is it possible for me to use ChangeHappend in my derived class. If so how? If not, what should I do instead? class Base { public delegate void ChangeHandler(object sender); public event ChangeHandler ChangeHappend; private int _foo; public int Foo { set { if (_foo == value) return; ...

Using Action<T> as an argument in C# (mimicking a function pointer)

I need to write a delegate function that can 'wrap' some while/try/catch code around a basic UDP call to verify the link. I made it work for Func for a function that has no arguments, but I can't make it work for Action, which has an argument (but no return). I can't seem to pass in the argument in a logical way without the compiler comp...

Issue with Callback method and maintaining CultureInfo and ASP.Net HttpRuntime

Hi All, Here is my issue. I am working on an E-commerce solution that is deployed to multiple European countries. We persist all exceptions within the application to SQL Server and I have found that there are records in the DB that have a DateTime in the future! We define the culture in the web.config, for example pt-PT, and the forma...

A way to perform conversion between open and closed delegates

I need to convert an open delegate (one in which the Target is not specified) into a closed one efficiently. I have profiled my code, and the cost of using CreateDelegate() to produce a closed delegate for an instance method is a significant fraction (>60%) of the overall run time (as it takes place for each new instance of the type). ...

Should you set the delegate to nil in the class using the delegate or in the class itself

If class A is using class B and class A is class B's delegate, is it ok if the delegate is set to nil in class B's dealloc? I have seen code usually resetting the delegate to nil inside class A's dealloc but wasn't sure the real difference doing it one way or the other. e.g. This is the usual way: // somewhere in class A - (void) some...

Function Pointers in Java

This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java? Given that pointers are somewhat absent, what is the best way about this? And to be clear, ...

OCUnit test for protocols/callbacks/delegate in Objective-C

Using OCUnit, is there a way to test delegate protocols? I'm trying this, which doesn't work. -(void) testSomeObjDelegate { SomeObj obj = [[SomeObj alloc] initWithDelegate:self]; [obj executeMethod]; } -(void) someObjDelegateMethod { //test something here } I'm going to try calling the obj method on a different thread and have ...

Creating a function dynamically at run-time

It probably isn't even possible to do this, but I will ask anyway. Is it possible to create a function that receives a string and then uses it as a right side argument for the goes to operator (=>) used in lambda? Actually, what I want to do is to be able to redefine an specific method of a specific class during runtime. I want to be wr...

How to expose and raise custom events for a vb.net winforms user control

Hello, Please read THIS post. I have the same problem as described in this post but I am trying to do in in VB.net rather than c#. I am pretty sure to do this I have to use a custom event. (I used a code conversion site to get to learn about custom events.) So in the IDE when I type the following: Public Custom Event AddRemoveAtt...

Uses of delegates in C# (or other languages)

I have always wondered how delegates can be useful and why shall we use them? Other then being type safe and all those advantages in Visual Studio Documentation, what are real world uses of delegates. I already found one and it's very targeted. using System; namespace HelloNamespace { class Greetings{ public static void Displ...

Exception/Error handling in Objective-C (iPhone app)

I actually have two questions regarding exception/error handling in the iPhone app that I am making: The app uses Internet, but when there's no connection, the app just dies (during launch). How can I handle this to print some infomsg to the user, instead of just getting thrown back to the springboard? Can someone show me an example of...

How to convert delegate to object in C#?

Hello, I am using reflection class to invoke some methods which are on the some other dll. And one of the methods' parameters are type of delegate. And I want to invoke this methods by using reflection. So I need to pass function parameters as object array, but I could not find anything about how to convert delegate to object. Thanks ...

Most Concise way to Invoke a void Method Asynchronously

Hi all, I have a method which I would like to invoke asynchronously: void Foo() { } I can indeed invoke this asynchronously by the following: delegate void DVoidMethod(); DVoidMethod FooDelegate = new DVoidMethod(Foo); FooDelegate.BeginInvoke(null,null); Has anyone got any alternatives? I think three lines of code is too much? ...

Using an IEnumerable<T> as a delegate return type

I'm trying to define a delegate function that will return an IEnumerable. I'm having a couple issues - I think I'm close, but need some help getting there... I can define my delegate fine: public delegate IEnumerable<T> GetGridDataSource<T>(); Now how do use it? // I'm sure this causes an error of some sort public void someMethod...

Setting a generic delegate to a class-level variable

I bumped into an additional question that I needed in regards to this: http://stackoverflow.com/questions/1099729/using-an-ienumerablet-as-a-delegate-return-type From the above solution, the following was suggested: class Example { //the delegate declaration public delegate IEnumerable<T> GetGridDataSource<T>(); //the gene...

Logical error or is my program skipping parts?

FYI I'm programming in objective-C but anyone might be able to help. I've been sitting here for the past two hours trying to figure out what the problem is and I've done everything I know to debug this simple little coding problem. Check out the code below and I will explain. In the application, it starts out with MainScreenViewControlle...

Unmanaged code calling vb.net callback

I am upgrading vb.net app that handles the events of a COM object (possibly written in VB6) from framework 1.1 to WPF 2.0/3.5 The code: (object names simplified for brevity) public class MyClass Private WithEvents serviceMonitor As COMOBJECT.ServiceMonitor Public Sub New() serviceMonitor = New COMOBJECT.ServiceMonitor()...

Is this bad design?

I have a control and it provides a selection mechanism. However, the items that it selects are not appropriate for general consumption and instead, must be projected into a different type parameterised by date. In addition to this, there are some controls that want to encapsulate the same information and, more importantly, want to retai...