E
E
evgenyt20002018-10-22 10:29:27
C++ / C#
evgenyt2000, 2018-10-22 10:29:27

Is it possible to declare a delegate in one class (methods in the same place), and create a variable, assign the address of a method, call it in another class?

Is it possible to declare a delegate in one class (methods in the same place), and create a variable, assign the address of a method, call it in another class?
The situation is as follows:
There is a namespace and two classes, in the second lane. All methods are in the first class. And I want to declare a delegate, etc. there, but use it in another class, but somehow it doesn’t work, all the examples that I saw within the same class ... :-(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2018-10-22
@evgenyt2000

There is such a thing as "scope". If the delegate type is inside a class, then it can only be obtained through an instance of this class, i.e. through an object of the type of that class. To be able to create a delegate in another class, you must either remove the delegate from it, or, as I said, refer to an instance of the class.

A
Alexey Pavlov, 2018-10-22
@lexxpavlov

And who's stopping you from doing this?.. A delegate can be declared not in a class at all, but simply in a namespace. After all, a delegate is just a method signature (number and types of method arguments).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question