delegates

Why would I make an all-optional message protocol?

I'm writing a Cocoa API for a project and the API takes a delegate. The protocol that I came up with declares all the methods as optional, but why would I do that instead of just documenting the delegate methods in a header file and taking a plain id as a parameter? ...

Composing of delegates (functional pitfall)

When trying to use delegates in C# to solve a problem in a functional way, I've come across a pitfall that I want to share resp. for which I would like to hear your suggestions. Background I want to fill a grid from a list of objects where the values for single columns are get using delegates (idea borrowed from Philip Pipers ObjectLis...

Is there anything you can do with a C# event that you cannot do with a VB Event?

My friend is primarily a VB developer and he says time and time again how much more simple it is to code events in VB than C#. My take on the issue is that it probably is easier but if there was not a reason for the added complexity, they probably would have made it just as simple in C#. Can anyone tell me if there is any added flexibili...

Will an empty delegate eat up memory?

public sealed class FtpManager { public event EventHandler LoggingIn = delegate { }; private void OnLoggingIn(object sender, EventArgs e) { var handler = LoggingIn; handler(sender, e); } // ... } In the above code, I have initialized LoggingIn event handler with an empty delegate. Will that affect memory...

C# Typeresticted delegate with Template

The .net EventHandler is limited to Templates that inherits from EventArgs. How is that done? The implementation (Got to refference in vs) shows the following code: [Serializable] public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e); But i think TEventArgs is just a name. How can I write a typed delegate that is...

Extending c# code with separate assembly

I have some extra functionality i need to add which includes adding a new property to an object and extending methods in another class that handles this object. I'm dealing with source code of a product (in C# 2.0) we use which i really don't want to modify, i just want to extend on it. I'd ideally like to have a separate assembly to d...

C#: Passing a delegate with two parameters as a parameter function

I have a sequence of functions that look very similar but for a single line, like the following two (but I have many more of them): private static int HowManyHoursInTheFirstYear(IList<T> samples) { DateTime firstDate = samples[0].Date; int count = 0; while (count < samples.Count && samples[count].Date.Year == fir...

How do I load images in the background?

I am trying to load an image in the background and then update the UI. I have been playing with this all day and I don't know what I am missing. I keep getting the following error: "The calling thread cannot access this object because a different thread owns it." I've hunted around following example after example, but I cannot se...

C# dictionary<> missing key

When i do val = dict["nonexistent key"] i get System.Collections.Generic.KeyNotFoundException Is there a way i have my dictionary call a member function with the key as a param to generate a value? -edit- Maybe i should of been more specific. I want to AUTOMATICALLY call a member function to do what it needs create the proper value for ...

Events - naming convention and style

Hi I'm learning about Events / Delegates in C#. Could I ask your opinion on the naming/coding style I've chosen (taken from the Head First C# book)? Am teaching a friend about this tomorrow, and am trying to come up with the most elegant way of explaining the concepts. (thought the best way to understand a subject is to try and teach ...

How do I create a delegate for a .NET property?

Hi, I am trying to create a delegate (as a test) for: Public Overridable ReadOnly Property PropertyName() As String My intuitive attempt was declaring the delegate like this: Public Delegate Function Test() As String And instantiating like this: Dim t As Test = AddressOf e.PropertyName But this throws the error: Method 'Pub...

Does the @ prefix for delegates have any special meaning?

Several times I've seen ReSharper generate code that looks like this: delegate void myHandler(int i); myHandler myHandlerContainer; ... foreach (Delegate @delegate in myHandlerContainer.GetInvocationList()) {...} Does the '@' in @delegate give that variable any special semantic meaning? Or is it just a convention I didn't encounter be...

Standard delegates in C#

There are some Delegates predefined in C# I know these: EventHandler // Default event callbacks EventHandler<T> // Default event callbacks with custom parameter (inheriting from EventArgs) Action // Function without return value and without parameter Action<T1, T2, T3, T4> // Function without return value and 1-4 parameters Func<T1, T2...

Store and invoke an event (VB.NET .NET 2.0)

Hi, I think know how to do this in C# but I'm having syntax trouble in VB.NET because I need to take advantage of the 'handles' event stuff. I have different events that I somehow (probably delegate) need stored in a property of an attribute (I just add an attribute to a property linking to the corresponding event). E.g. <BindEvent(E...

Can a C# delegate use the object type to be more generic?

I would like to create a delegate and a method that can be used to call any number of Web services that my application requires: Example: public DateCheckResponseGetDate(DateCheckRequest requestParameters) { delegate object WebMethodToCall(object methodObject); WebMethodToCall getTheDate = new WebMethodToCall(WebServices.GetTh...

Is it at all possible to extend System.Delegate?

It is not possible to inherit from System.Delegate or System.MulticastDelegate in C#. It is perfectly possible to do it in MSIL as long as you declare standard 'runtime managed' methods. However, every time I am adding a 'cil managed' method to the type, I am getting: System.TypeLoadException: Illegal definition for runtime implemented ...

Winforms to WPF conversion: BeginInvoke to what?

Hi all, Here's my old code from WinForms: private void ValueChanged(double inValue1, double inValue2) { //only manual mode for this driver, so that's easy. if (ValueLabel.InvokeRequired) { ValueLabel.Invoke(new MethodInvoker(delegate { ValueLabel.Text = (inValue1* inValue2/ 1000).ToString...

How to pass a method name in order to instantiate a delegate?

In the following example, I want to define a System.Action which executes a specific method that I define at runtime, but how do I pass the method name (or the method itself) so that the Action method can define the delegate to point to that particular method? I'm currently getting the following error: 'methodName' is a 'variable' but...

Is there a way to call a method when any property of a class is set?

So what I'm trying to do is call a single propertyWasSet() function when any property within a C# class is set (conversely, propertyWasGot() when it is get). I would also like to know which property's 'get' was invoked. I would like to maintain a dictonary of properties that are 'set', and check upon the 'get' action if they have been s...

Func<TObject, bool> or Predicate<TObject>?

I've seen time and time again API (particularly in the .NET framework) that uses Func<TObject, bool> when Predicate<TObject> is seemingly a perfectly responsible option. What good reasons might an API designer have for doing so? ...