Questions tagged [delegates]

Delegates can refer to several concepts. An object can rely on another (a delegate) to perform a function. Delegation can also refer to programming language feature making use of the method lookup rules for dispatching self-calls. In C#, a delegate defines which method to call when an event is triggered.

In object-oriented programming, there are three related notions of delegation.

Most commonly, delegation refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls. Delegation as a language feature supports the prototype-based programming model.

Delegation can also refer to one object relying upon another to provide a specified set of functionalities. In research, this is often referred to as consultation or as aggregation in modeling.

In C#, a delegate is a way of telling which method to call when an event is triggered, keeping the method type.

A Delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure.

Official Source

11320 questions
4
votes
2 answers

SwiftUI Map Overlays without UIViewRepresentable

I try to create Map view with annotations and overlays using only iOS 14 SwiftUI Map. I managed to create annotations but I am not able to create visible overlays because of lack of MKOverlayRenderer from MKMapView delegate. How can I attach to Map…
Paweł Madej
  • 1,229
  • 23
  • 42
4
votes
1 answer

Intercepting a method with reflection

I am trying to intercept a method through a proxy class and am getting a TargetException "Object does not match target type". I believe this is similar to what a framework like PostSharp does, but I want to see if I can do this myself as an…
Sean Thoman
  • 7,429
  • 6
  • 56
  • 103
4
votes
3 answers

AVAudioPlayer and AVAudioRecorder: Delegate Methods not Called

My delegate methods audioRecorderDidFinishRecording and audioPlayerDidFinishPlaying are not being called. Those methods should trigger a 'stopanimation` method that stops an animation after recording is finished. I have placed a call call to the…
Heena Dave
  • 319
  • 7
  • 21
4
votes
1 answer

Is it possible to have a ShareItem call a method when sharekit finishes sharing?

When the user posts to FB or Twitter I want to know that the share completed, or the view was dismissed with an x. Does anyone know if there is a delegate method built into ShareKit or if I have to write my own methods into it? Right now I'm using…
Joshua
  • 709
  • 2
  • 10
  • 23
4
votes
2 answers

How to find which NSTextfield is focused

I'm having a problem finding out which NSTextfield is focused. I am building a multi-language form and have several NSTextfields for data entry. I have to change the text input source for some of the NSTextfields during data entry, and I need it to…
Prooshani
  • 534
  • 7
  • 20
4
votes
1 answer

VB.Net why is this not a bug?

I encounter what I believe to be a bug and I was just wondering if this is already known as a issue or if this is not a issue and why. The problem related to Read Only Properties on a Type when compiling with the VB.Net Compiler in Visual Studio…
Jay
  • 3,276
  • 1
  • 28
  • 38
4
votes
2 answers

Can a multicast delegate in C# 4.0 be created from some sort of collection of method names?

I'm writing a simple game in XNA and I've faced a problem with delegates. I use them to represent physics in the game, e.g.: public delegate void PhysicsLaw(World world); //for gravitation static public void BallLawForGravity(World world) …
Lakret
  • 127
  • 2
  • 7
4
votes
2 answers

C# dont understand covariance and contravariance of delegates

For a long time now I have been trying to understand the usefulness of the "in" and "out" parameters in connection with generics in C # and I just can't get it into my head(I know how often this question is asked on StackOverflow). I generally…
Coco07
  • 43
  • 5
4
votes
5 answers

Difference in invoking through Delegates and Events C#

What is the difference? Using Delegate public delegate void TestDelegate(); public TestDelegate delObj = SomeMethod; public void SomeMethod() { ..... } public void Test() { if(delObj != null) delObj(); } Using Event public…
Nishant
  • 905
  • 1
  • 16
  • 36
4
votes
3 answers

How to call UIButton action from view controller to another view controller in iOS Swift?

In Webview Controller, I have UIButton action to perform save functionality. I can able to perform actions inside the web view controller. In my case, want to perform a save action of a webview controller from cardViewController. But nothing…
PvUIDev
  • 95
  • 1
  • 9
  • 38
4
votes
1 answer

JUnit assert that an Activity is implementation of an interface

I have an Android Activity that implements an delegate interface which is called IMeetingRoomDelegate interface IMeetingRoomDelegate { fun onMeetingRoomFragmentClicked(homeFragment: MeetingRoomHomeFragment, meetingRoom:…
Marc Freeman
  • 713
  • 2
  • 7
  • 30
4
votes
1 answer

Why aren't delegate instances always cached?

Why does the C# compiler not create code which caches the delegate instance of Action(SomeMethod) in this case: void MyMethod() { Decorator(SomeMethod); } void Decorator(Action a) { a(); } void SomeMethod() { } It does it only when SomeMethod…
Don Box
  • 3,166
  • 3
  • 26
  • 55
4
votes
4 answers

How to call delegate from string in C#?

is it possible to call a delegate stored in a variable by its variable name (as a string)? i guess i'd have to use reflection mechanism, but i'm not getting anywhere example code: class Demo { public delegate int DemoDelegate(); private static…
knittl
  • 246,190
  • 53
  • 318
  • 364
4
votes
1 answer

Generalizing delegate code (C#)

I have two very similar classes that do essentially the same thing. The only difference is in a callback handler provided to an instance of each class. The callback handlers are different and they are accepted with different parameters. I would like…
kateroh
  • 4,382
  • 6
  • 43
  • 62
4
votes
2 answers

Video capturing in iOS with MonoTouch

I have the code to create, configure and start a video capturing session in Objective-C running without problems. I'm porting the sample to C# and MonoTouch 4.0.3 and have a few problems, here is the code: void Initialize () { //…
Pedro Oliveira
  • 140
  • 1
  • 8