delegates

C# Dynamic Type Initializer

I'm trying to build something like the C# type initalizer dynamically: MyClass class = new MyClass { MyStringProperty= inputString }; I want to build a generic method that reflects over a given type once and returns a delegate which creates a new instance of the class and populates it based on the input parameter. The method signature...

Why can't a delegate refer to a non-static method when used in a static method?

Why is it necessary to make a function STATIC while using delegates in C# ? class Program { delegate int Fun (int a, int b); static void Main(string[] args) { Fun F1 = new Fun(Add); int Res= F1(2,3); Console.WriteLine(Res); } **static public int Add(int a, int b)** { int result; ...

ABPeoplePicker Delegate pattern question

I have a subclassed UIViewController that's acting as an ABPeoplePicker Navigation Controller Delegate. This view controller calls this ABPeoplePicker in a few different situations and the problem I'm having is figuring out which situation I'm responding to in - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController...

Is there a difference between only <NSXMLParserDelegate>, only setDelegate method, or the use of both of them ?

Hey, I noticed that when I do this : [myParser setDelegate:self]; it works :D (even if I didn't add the code in the header file ... you know, the <delegateStuff, delegateOtherStuff> in the interface declaration) When are you supposed to modify the header file to make your delegate work ? Is the setDelegate method enough to ma...

C# : Creating Events for a Class on GET/SET properties

I'm looking to fire an event each time a property within my class is SET. I'd like to be able to trigger this same event whenever one of my properties are set. I have (around 12 of them) I.e. public class MyClass { private int _myHeight; private int _myWidth; public int myHeight { get { return myHeight; } set { myHei...

Capturing delegates in anonymous methods

Consider Action _captureAction; private void TestSimpleCapturedAction() { Action action = new Action(delegate { }); Action printAction = () => Console.WriteLine("Printing..."); action += printAction; CaptureActionFromParam(action); action -= printAction; _captureAction(); //p...

Why isn't this Silverlight attached property working?

I'm trying to use the MVVM pattern in my Silverlight 3 application and am having problems getting binding to a command property of a view model working. First off, I'm trying to add an attached property called ClickCommand, like this: public static class Command { public static readonly DependencyProperty ClickCommandProperty = ...

Using one delegate to for several methods with different parameters.

Hi! Is it possible to use one delegate for several methods with different parameters somehow? I use reflection to get all the methods in a class, and I want to assign each of them a delegate and save that delegate in a dictionary with an enum as the key. This if for a remote procedure call implementation I'm working on so the enum is a...

Co- and Contravariance bugs in .NET 4.0

Some strange behavior with the C# 4.0 co- and contravariance support: using System; class Program { static void Foo(object x) { } static void Main() { Action<string> action = _ => { }; // C# 3.5 supports static co- and contravariant method groups // conversions to delegates types, so this is perfectly legal: action...

Delegate: Method name expected error

Hi, I'm trying to get the following simple Delegate example working. According to a book I've taken it from it should be ok, but I get a Method name expected error. namespace TestConsoleApp { class Program { private delegate string D(); static void Main(string[] args) { int x = 1; ...

iphone tableview cells with custom textview - get textview reference

Hi guys I have a UITableView with 15 cells, each with a separate text box in it. I have implemented UITextViewDelegate and I am able to received changed textview data using textViewDidChange (etc). But I have one big problem still, how do I know WHICH textview sent this, (i.e. in which cell was the textview altered?) Its interesting to...

How do you trigger an event across classes?

I am writing a Class Library that will be used by other applications. I am writing it in C#.NET. I am having a problem with triggering events across classes. Here is what I need to do... public class ClassLibrary { public event EventHandler DeviceAttached; public ClassLibrary() { // do some stuff OtherClass....

How do you trigger an event across classes?

I am writing a Class Library that will be used by other applications. I am writing it in C#.NET. I am having a problem with triggering events across classes. Here is what I need to do... public class ClassLibrary { public event EventHandler DeviceAttached; public ClassLibrary() { // do some stuff OtherClass....

Delegates in VB - how to from C#?

I'm stuck on converting this C# code to VB: AsyncHelpers.Async.Do(delegate { WriteStuff("additional stuff..."); }); I was thinking it would be something like AsyncHelpers.Async.Do(Function() WriteStuff("additional stuff...") but I get the error "Expression does not produce a value". Can someone please give me the appropriate VB.Ne...

Anonymous delegates don't seem to enforce type-checking

I've put together a small code-sample below (Currently in C# 3.5 but would also like to know if the answer is any different in C# 4.0) I have three simple delegates, and three simple functions ... No problem here, everything compiles as expected, and will not compile if I accidentally try to link delegate A with Method B etc (wrong numb...

What is the best way to do multithreading or asynchronous task in .NET with return value?

What would be the best way to do multithreading or asynchronous task in the following situation in C#? The simplified situation: A http request needs to make 5 or more web service calls. Upon completion each web service call will receive and return a string list as a result. The caller (of 5 web service calls) need to merg...

Determine if object is any Predicate<T>

I've got an IList<Delegate> that contains some Func<bool>s and some Predicate<T>s, where T varies. I later need to sort out which of these items are Predicate<T>s, but don't want to close the door to adding other Delegate types to the list later, so I do not want to do this by identifying objects by !(current_delegate is Func<bool>). Th...

How do i implement this delegate?

Action doesnt seem to support params string[] as a param so i wrote delegate void WriteFn(string s, params string[] ls); i have this function void blah(WriteFn Write, string fmt, params string[] a) Now i would like to write an function but i cant seem to figure the syntax out. It something like { var sw = ... blah(new Writ...

Invoke delegates without params but using local params c#

I find myself doing the following a lot, and i don't know if there is any side effects or not but consider the following in a WinForms C# app. (please excuse any errors as i am typing the code in, not copy pasting anything) int a = 1; int b = 2; int c = 3; this.Invoke((MethodInvoker)delegate() { int lol = a + b + c; }); Is there ...

Lazy generic delegate initialisation using Ninject

I'm using Ninject 1.0 and would like to be able to inject lazy initialisation delegates into constructors. So, given the generic delegate definition: public delegate T LazyGet<T>(); I'd simply like to bind this to IKernel.Get() so that I can pass a lazy getter into constructors, e.g. public class Foo { readonly LazyGet<Bar> getBa...