delegates

Checking delegates for null

I was reading the Essential C# 3.0 book and am wondering if this is a good way to check delegates for null?: class Thermostat { public delegate void TemperatureChangeHandler ( float newTemperature ); public TemperatureChangeHandler OnTemperatureChange { get; set; } float currentTemperature; public float CurrentTempera...

How can I get this dynamic WHERE statement in my LINQ-to-XML to work?

In this question Jon Skeet offered a very interesting solution to making a LINQ-to-XML statement dynamic, but my knowledge of lambdas and delegates is not yet advanced enough to implement it: I've got it this far, but of course I get the error "smartForm does not exist in the current context": private void LoadWithId(int id) { XDoc...

anonymous delegates in C#

I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code: private void RefreshForm() { if (InvokeRequired) Invoke(new InvokeDelegate(Refresh)); ...

Add an event to a class method?

OK, I've tried but I just don't get it. I have two classes logger and class1. I have a method called logger.Write(string) and method called class1.Execute(). Now in my application I want to have logger.Write(class1.ToString()) run when class1.Execute() is called. I presume you need to add delegates and events, but I just can't get my...

How do I find the index of an undefined string in a List<T>

It's my understanding that if I want to get the ID of an item in a list, I can do this: private static void a() { List<string> list = new List<string> {"Box", "Gate", "Car"}; Predicate<string> predicate = new Predicate<string>(getBoxId); int boxId = list.FindIndex(predicate); } private static bool getBoxId(string item) { ...

Using BackgroundWorker to update the UI without freezes...?

I have the following code for population a ListView from a background thread (DoWork calls the PopulateThread method): delegate void PopulateThreadCallBack(DoWorkEventArgs e); private void PopulateThread(DoWorkEventArgs e) { if (this.InvokeRequired) { PopulateThreadCallBack d = new PopulateThreadCallBack(this.PopulateTh...

iphone: delegate questions (I think)

I've taken some stuff from Apple's QUartzDemo project and am trying to modify it. I want to create a screen with a button that will then call the rest of the stuff to make the rectanlges from that demo. I've created a UIViewController that handles my initial view. It has a start button on it. The start button code is: - (IBAction)st...

pushViewController problem...

I create a UIViewController with a button on it. The button's only job is to create a window for drawing some rectangles. (I've taken some code from "QuartzDemo" from Apple's example applications.) Objective C is really giving me fits... When I try this, nothing happens. Here's the code for the button: - (IBAction)startButtonAct:(i...

How do I forward declare a delegate in C++/CLI?

How? The following did not work: delegate MyDelegate; ref class MyDelegate; delegate void MyDelegate; The following works for declaration: public delegate void MyDelegate(Object ^sender, MyArgs ^args); But using it as a forward declaration gives me error C3756: 'MyNameSpace::MyDelegate': delegate definition conflicts with an exi...

Anonymous method as parameter to BeginInvoke?

Why can't you pass an anonymous method as a parameter to the BeginInvoke method ? I have the following code: private delegate void CfgMnMnuDlg(DIServer svr); private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.Begin...

Nested Generic Events

I want to have a generic event that I can fire that will take a custom eventArgs> e Here is my code so far public event resultsEventHandler<T> returnResults; public delegate void resultsEventHandler<T>(object sender, resultEventArgs<ObservableEntityCollection<T>> e); protected virtual void OnreturnResults(resultEventArgs<ObservableE...

Interaction between two user controls

I'm on the verge of madness ... In the application I'm actually building, I'm dealing with two dynamically-added controls that need to interact with each other, but I've reduced the problem to an as-simple-as-I-can-make-it example with the controls being statically loaded, and it still presents the same problem: a NullReferenceException...

What is the accepted way to dynamically change the function of a delegate in .NET?

Suppose I have a class that represents a product to be priced using one of a number of different pricing strategies. This pricing occurs hundreds of times per second, so to eliminate repetitive if/else statements I am instead using a delegate to launch the appropriate strategy, like so: Private Delegate Sub PricingModel(ByVal params As ...

Extension methods defined on value types cannot be used to create delegates - Why not?

Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return num + num; } } Func<string> aaMaker = "a".Double; Func<string, string> doubler = FunnyExtensio...

Using delegates or interfaces to decouple the logging - Best practices - C#

My solutions has several projects which includes several libraries and one project for UI. Currently it is a windows forms application and I use log4net for logging. This UI project has only reference to log4net and this project maintains the configuration files. But I would like to log from my libraries as well. Usual method for doing...

Observer pattern implemented in C# with delegates?

There is a question already answered which is http://stackoverflow.com/questions/32034/in-c-isnt-the-observer-pattern-already-implemented-using-events It asks if the observer pattern is already implemented in c# using events. While I get the events and observer pattern, isn't the observer pattern really just delegates and events is a f...

In C# 3.5, How do you pass which method to call on an object as a parameter

I have two methods in C# 3.5 that are identical bar one function call, in the snippet below, see clientController.GetClientUsername vs clientController.GetClientGraphicalUsername private static bool TryGetLogonUserIdByUsername(IGetClientUsername clientController, string sClientId, out int? logonUserId) { string userna...

Debugger does not hit breakpoint (C#)

Hello. I found something quite odd(I think!). If I try to put a breakpoint in the yes() method, it will never pause the program when it executes the function. If I try to do the same to any other line of code, it will work just as expected. Is it a bug, or is there something that's escaping me? The filter will return the 2 objects, ever...

Genericising delegate/IAsyncResult calls

I have a WCF web service that currently searches multiple, hard-coded dtSearch indices and then merges the resulting datasets to be returned back to the client. I have the following C# code: public class Search : ISearch { delegate DataTable PDelegate(string term, int cid); delegate DataTable CDelegate(string term, int sid); ...

How does a delegate work in objective-C?

Does anyone know where I can find a good explanation/tutorial of what and how an application delegate works in objective-C? The two books I have don't dwell on delegates enough and do not explain them very well for me to truly understand their power and function. ...