63
голосов
8ответов
21402 просмотров

a constructor as a delegate - is it possible in C#?

I have a class like below: class Foo { public Foo(int x) { ... } } and I need to pass to a certain method a delegate like this: delegate Foo FooGenerator(int x); Is it possible to pass the constructor directly as a FooGenerator value, without having to type: delegate(int x) { return new ...

5
голосов
5ответов
1865 просмотров

Question about foreach and delegates

Suppose the following code: foreach(Item i on ItemCollection) { Something s = new Something(); s.EventX += delegate { ProcessItem(i); }; SomethingCollection.Add(s); } Of course, this is wrong because all the delegates points to the same Item. The alternative is: foreach(Item i on Ite...

0
голосов
1ответов
577 просмотров

Use reflection stub to initialize a delegate field lazily

The problem: a .Net 2.0 class with a few thousand delegate fields generated by a code generator varying signatures delegates may or may not return values no generics these delegates much be initialized quickly at runtime initializing a delegate is simple but expensive initializing the ...

1
голосов
2ответов
150 просмотров

ASP.NET Delegates and Expressions -Information Request

Based on my understanding , i interpret the meaning of Func delegate as follows.Please correct them as and when it is needed. Declaration : Func<int> dg ; 1. Could i interpret it as "a Delegate pointing to a method that returns an integer?". Declaration : Func<int,int> del...

3
голосов
3ответов
1364 просмотров

Cast between function pointers

I am currently implementing a timer/callback system using Don Clugston's fastdelegates. (see http://www.codeproject.com/KB/cpp/FastDelegate.aspx) Here is the starting code: struct TimerContext { }; void free_func( TimerContext* ) { } struct Foo { void member_func( TimerContext* ) { ...

1
голосов
1ответов
117 просмотров

C# OnPropertyChangedEvents Implementation and Delegate Question

I have a progress bar on a UI I would like to update when a record gets inserted into my database. I have done this before when I only had a UI Tier and a Business Tier with no problems. But I am now breaking my code into a UI Tier, Business Tier, and Data Tier and I can't figure how to properl...

8
голосов
4ответов
6250 просмотров

Does using a delegate create garbage

I'm working on a game for the xbox360, using XNA. On the Xbox the garbage collector performs rather badly compared to the one on a PC, so keeping garbage generated to a minimum is vital for a smoothly performing game. I remember reading once that calling a delegate creates garbage, but now for t...

2
голосов
2ответов
2886 просмотров

How can I get delegates to property accessors from a generic type?

I'm currently building a node editor (as in Blender) and am having trouble getting delegates to property accessors from a generic type. So far the question here has brought me closest, but I'm having trouble that I think is specifically related to the type of object being generic. For reference,...

3
голосов
3ответов
1088 просмотров

When does the vaIue of the InvokeRequired property change?

When i want to use delegate class to make call while windows form working, i always have to use InvokeRequired. It is ok. But who changed the InvokeReuqired property while it is working. Please check this image:

1
голосов
3ответов
323 просмотров

How to make Action<param> compatible with event's delegate type?

Given the code below: void LookupBox_Load(object sender, EventArgs e) { Action d = delegate { if (!_p.AutoClose) CloseLookupBox(); }; if (this.ParentForm.MdiParent != null) this.ParentForm.MdiParent.Deactivate += delegate { d(); }; ...

2
голосов
3ответов
853 просмотров

Is this delegate usage good or bad?

This question is being asked because I have no prior experience with delegate best practices I have a unordered lists in html, where the structure is the same throughout my site, but the content of the lists may differ. Examples: List of object A &lt;ul&gt; &lt;li&gt; &lt;ul&gt; &l...

0
голосов
5ответов
1147 просмотров

How to execute a callback method instead of an anonymous method?

The following example works, but how can I change it so that instead of executing the anonymous method, it executes my existing callback method OnCreateOfferComplete()? using System; namespace TestCallBack89393 { class Program { static void Main(string[] args) { ...

0
голосов
1ответов
484 просмотров

Comparison Delegate flip flops results on identical keys

Is there any way to get consistent results from Comparison when some of the sort items are the same? Do I just have to code up my own sort routine? public class Sorter { public void SortIt() { var myData = new List&lt;SortData&gt;(3); myData.Add(new SortData { Sort...

5
голосов
3ответов
1259 просмотров

C# print a delegate

Is there a simple way to print the code of a delegate at runtime ? (that "contains one method"). public delegate void SimpleDelegate(); SimpleDelegate delegateInstance = delegate { DoSomeStuff(); int i = DoOtherStuff() }; Now, I would like to display on the screen the body of dele...

6
голосов
6ответов
4401 просмотров

Events/Delegates In Java or C#

I've been trying to learn about events/delegates, but am confused about the relationship between the two. I know that delegates allow you to invoke different functions without needing to know what particular function is being invoked. (eg: a graphing function needs to accept inputs that are diffe...

2
голосов
3ответов
3720 просмотров

Why does marshalling a struct of callback delegates cause an AccessViolationException

Introduction I am trying to use P/Invoke to register a struct of callbacks with a native dll. When calling a function that makes the native dll invoke the callbacks an AccessViolationException occurs. I have constructed a "small" test case that demonstrates the behavior comprised of 2 files, nat...

2
голосов
2ответов
1500 просмотров

Convert C# statement body lambda to VB

It appears that VB in VS8 doesn't support/convert lambda expressions with a statement body. I'm using them in my C# application, but now must convert it to VB. I'm creating a whole bunch of controls dynamically, and I want to be able to give them event handlers on the fly. This is so I can bui...

5
голосов
2ответов
1400 просмотров

A general delegate type for handling any event

I have a function that receives two parameters - an object, and an EventInfo structure defining an event on that object. I need to block that function until the specified event fires. The problem I have is, how do I add a delegate to the specified event, when the type of handler could be anythin...

0
голосов
5ответов
326 просмотров

Lambda expression - How to work on delegates in C#?

I took the following example from Jrista's answer to a post. Finding Twentyone count int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 }; var twentyoneCount = numbers.Where(n =&gt; n == 21).Count(); Suppose i use "Func" delegate how can i get the count ? I tried as (par...

0
голосов
4ответов
202 просмотров

Best way to get object data?

I need to display some stats, numbers, and graphs about various game objects on the screen. (examples: camera position, field of view, frames per second, fill rate, number of objects culled, etc... ) Currently any object which wants to be graphed or displayed implements an interface along thes...

1
голосов
3ответов
98 просмотров

Handling Parameters in Threads

I am just a beginner."ParameterizedThreadStart" accepts single object as argument. Is there any other delegate signature allows me to (1) pass params (variable number of parameter) on thread? (2) support generic parameters like list ?

0
голосов
1ответов
1007 просмотров

didReceiveData of NSMutableUrlRequest never triggered

I’m new in iphone development world and I’m trying to call a web service using the HTTP method POST. To do that I'm using the NSMutableUrlRequest. My problem is that the DidReceiveData delegate is never called and the NSUrlConnection doesn’t return null. Here is my code : - (void)connection:(...

4
голосов
3ответов
3708 просмотров

C#: Can I cast an explicit delegate to an Action delegate?

Given: delegate void Explicit(); Can I: public void Test(Explicit d) { Action a; a = d; // ???? } I have a scenario where I need to overload a constructor that has: public MyClass(Expression&lt;Action&gt; a) {} but the following overload is ambiguous: public MyClass(Action a) {...

0
голосов
1ответов
983 просмотров

AvAudioPlayer setting delegate to nil releases the delegate object?

@implementation MyClass -(id) init { NSString *path0 = [ [NSBundle mainBundle] pathForResource:@"myfile" ofType:@"m4a" ]; mSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path0] error:NULL]; mSound.delegate = self; } -(void) release { mSound.deleg...

2
голосов
1ответов
2112 просмотров

function.createdelegate

how can i call 2 methods using function.createdelgate() lik i'm having 2 methods (method_one and method_two) Function.CreateDelegate(this,method_one); but i need to call both the methods in it...

69
голосов
9ответов
31043 просмотров

How do you use Func&lt;&gt; and Action&lt;&gt; when designing applications?

All the examples I can find about Func&lt;&gt; and Action&lt;&gt; are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. ...

11
голосов
3ответов
15785 просмотров

Subscribe to events within a WCF service

I have a need to do some real-time reporting on the functionality of a WCF service. The service is self-hosted in a windows app, and my requirement is to report "live" to the host app when certain methods are called by the client. My initial thought on the task was to publish a "NotifyNow" even...

12
голосов
3ответов
5239 просмотров

What role do delegates play in dependency injection?

In most examples of dependency injection, I see simple objects being injected, such as in the example below SecurityManager gets injected into MainApplication. However, it would seem natural to inject delegates as well, as in the example below LogHandler gets injected into MainApplication. Are ...

1
голосов
2ответов
1182 просмотров

Calling Subroutines from lambda in vb.net

I find myself calling functions from lambdas frequently as the provided delegate does not match or does not have sufficient parameters. It is irritating that I cannot do lambda on subroutines. Each time I want to do this I have to wrap my subroutine in a function which returns nothing. Not pretty...

19
голосов
2ответов
7783 просмотров

Impossibly Fast C++ Delegates and different translation units

According to Sergey Ryazanov, his Impossibly Fast C++ Delegates are not comparable: My delegates cannot be compared. Comparison operators are not defined because a delegate doesn't contain a pointer to method. Pointer to a stub function can be different in various compilation units. To whic...