troubles with delegates - c#

There a class and a delegate C#
public delegate void Super();
public class Event
{
public event Super activate ;
public void act()
{
if (activate != null) activate();
}
}
and C++/Cli
public delegate void Super();
public ref class Event
{
public:
event Super ^activate;
void act()
{
activate();
}
};
in C# I create multicast delegate in the class like this(methods Setplus and setminus)
public class ContainerEvents
{
private Event obj;
public ContainerEvents()
{
obj = new Event();
}
public Super Setplus
{
set { obj.activate += value; }
}
public Super Setminus
{
set { obj.activate -= value; }
}
public void Run()
{
obj.act();
}
}
but in C++/Cli I've got an error - usage requires Event::activate to be a data member
public ref class ContainerEvents
{
Event ^obj;
public:
ContainerEvents()
{
obj = gcnew Event();
}
property Super^ Setplus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Combine(obj->activate,value));
}
}
property Super^ SetMinus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Remove(obj->activate,value));
}
}
void Run()
{
obj->act();
}
};
Where is the problem?

See: http://msdn.microsoft.com/en-us/library/ms235237(v=vs.80).aspx
C++/CLI follows the same analog as C#. It would be illegal to define this in C#:
public Super Setplus
{
set { obj.activate = Delegate.Combine(obj.activate, value); }
}
It is the same for C++/CLI. Use the +=/-= notation that is defined in the modern syntax.
property Super^ Setplus
{
void set(Super^ value)
{
obj->activate += value;
}
}

Related

Multiple generic event publishers?

I have this simple interface:
public interface IEventPublisher<T>
{
public delegate void EventHandler(T report);
public event EventHandler OnEventReceived;
}
Then I can write a class to subscribe to events of type int and string:
public class Subscriber
{
public Subscriber(IEventPublisher<int> intPublisher, IEventPublisher<string> stringPublisher)
{
intPublisher.OnEventReceived += OnIntEventReceived;
stringPublisher.OnEventReceived += OnStringEventReceived;
}
private void OnIntEventReceived(int report)
{
}
private void OnStringEventReceived(string report)
{
}
}
Now comes the challenge: I would like to write a class to be a publisher of events of type int and string, so I start with:
public class Publisher : IEventPublisher<int>, IEventPublisher<string>
{
}
How do I implement this in order to satisfy both interfaces? The problem is that the event handler names are identical (OnEventReceived).
This is how I would have written it for a publisher just implementing one of the interfaces:
public interface IEventPublisher<T>
{
public delegate void EventHandler(T report);
public event EventHandler OnEventReceived;
}
public class Subscriber
{
public Subscriber(IEventPublisher<int> intPublisher)
{
intPublisher.OnEventReceived += OnIntEventReceived;
}
private void OnIntEventReceived(int report)
{
}
}
public class Publisher : IEventPublisher<int>
{
public event IEventPublisher<int>.EventHandler? OnEventReceived;
public Publisher()
{
Subscriber subscriber = new Subscriber(this);
}
private void GenerateEvents()
{
while (true)
{
...
OnEventReceived?.Invoke(number);
}
}
}
Did you try it? My IDE (with ReSharper) gives this implementation:
public class Publisher : IEventPublisher<int>, IEventPublisher<string>
{
event IEventPublisher<int>.EventHandler? IEventPublisher<int>.OnEventReceived
{
add
{
// Your code here.
}
remove
{
// Your code here.
}
}
event IEventPublisher<string>.EventHandler? IEventPublisher<string>.OnEventReceived
{
add
{
// Your code here.
}
remove
{
// Your code here.
}
}
}
And the Subscriber class would subscribe like this.
public class Subscriber
{
public Subscriber()
{
var publisher = new Publisher();
IEventPublisher<int> intPublisher = publisher;
IEventPublisher<string> stringPublisher = publisher;
intPublisher.OnEventReceived += OnIntEventReceived;
stringPublisher.OnEventReceived += OnStringEventReceived;
}
private void OnIntEventReceived(int report)
{
}
private void OnStringEventReceived(string report)
{
}
}
Note: I haven't actually tried compiling/running this.

Why does event remain null even after subscribing to it?

I have classes Room and Iceball. Iceball and IceShard are inherited from class Magic.
When I add new magic to room, room subscribes to event CreateNewMagic magic, but after exiting from method event is still null.
Iceball after collision should creates ice shards in the room with the help of event, but because the event is null room cant spawn ice shards
Class Room
{
public readonly List<Magi> MagicInRoom;
public void SpawnMagic(Magic magic)
{
MagicInRoom.Add(magic);
magic.CreateNewMagic += SpawnMagic;
}
}
Class Magic, IceBall and IceShard
public abstract class Magic
{
public delegate void MagicHandler(Magic magic);
public event MagicHandler CreateNewMagic;
public virtual void OnCollisionEnter()
{
if (...)
{
...
}
}
}
public class IceBall : Magic
{
public delegate void MagicHandler(Magic magic);
public event MagicHandler CreateNewMagic;
public override void OnCollisionEnter()
{
if (...)
{
var iceShards = CreateIceShards();
foreach (var iceShard in iceShards)
if (CreateNewMagic != null) //ALWAYS IS NULL
CreateNewMagic(iceShard);
}
}
}
public class IceShard : Magic
{
...
}
Event should be abstract
public abstract class Magic
{
public delegate void MagicHandler(Magic magic);
public abstract event MagicHandler CreateNewMagic;
public virtual void OnCollisionEnter()
{
if (...)
{
...
}
}
}
public class IceBall : Magic
{
public override event MagicHandler CreateNewMagic;
public override void OnCollisionEnter()
{
if (...)
{
var iceShards = CreateIceShards();
foreach (var iceShard in iceShards)
if (CreateNewMagic != null) //ALWAYS IS NULL
CreateNewMagic(iceShard);
}
}
}
public class IceShard : Magic
{
public override event MagicHandler CreateNewMagic;
}

Pass event through classes C#

I have a first class that raises an event when some changes occur:
public class MyFirstClass {
public event EventHandler ChangesHappened;
public MyFirstClass() {
}
public void MyMethod() {
//Thing happened
ChangesHappened?.Invoke(this, new EventArgs());
}
}
I also have a second class that have a list of FirstClass:
public class MySecondClass {
private List<MyFirstClass> first;
public MySecondClass() {
foreach(var f in first) {
first.Changed += (s, e) => {};
}
}
}
And last I have a WPF application with an instance of SecondClass. How can I handle the Changed event (FirstClass) from WPF? Should I create an event in SecondClass and raise it inside first.Changed += (s, e) => { NewEvent(this, new EventArgs()); } and then catch this in the WPF?
The objective is get the Changed event in the WPF application.
It seems to me that this is the simplest answer:
public class MySecondClass
{
public event EventHandler ChangesHappened;
private List<MyFirstClass> first;
public MySecondClass()
{
foreach (var f in first)
{
f.ChangesHappened += (s, e) => ChangesHappened?.Invoke(s, e);
}
}
}
Another option is to use Microsoft's Reactive Framework which lets you pass events (well observables) around as first-class language citizens.
You could do this:
void Main()
{
var msc = new MySecondClass();
msc.Changes.Subscribe(ep =>
{
/* Do stuff with
ep.Sender
ep.EventArgs
from the `MyFirstClass` instances
*/
});
}
public class MyFirstClass
{
public event EventHandler ChangesHappened;
public IObservable<EventPattern<EventArgs>> Changes;
public MyFirstClass()
{
this.Changes = Observable.FromEventPattern<EventHandler, EventArgs>(
h => this.ChangesHappened += h, h => this.ChangesHappened += h);
}
public void MyMethod()
{
ChangesHappened?.Invoke(this, new EventArgs());
}
}
public class MySecondClass
{
public IObservable<EventPattern<EventArgs>> Changes;
private List<MyFirstClass> first = new List<MyFirstClass>();
public MySecondClass()
{
this.Changes = first.Select(f => f.Changes).Merge();
}
}
As #Enigmativity already mentioned: When you have a class, that has to manage other classes (bunch of MyFirstClass references) then you have to forward your events from sub class to manager class.
public class MySecondClass
{
public event EventHandler Changed;
private List<MyFirstClass> firstClassList;
public MySecondClass()
{
firstClassList = new List<MyFirstClass>();
}
public void AddMyFirstClassList(List<MyFirstClass> firstClassList)
{
foreach (var firstClass in firstClassList)
AddMyFirstClass(firstClass);
}
public void AddMyFirstClass(MyFirstClass firstClass)
{
// from sub class to manager class
firstClass.Changed += firstClass_Changed;
firstClassList.Add(firstClass);
}
private void firstClass_Changed(object sender, EventArgs args)
{
Changed?.Invoke(sender, args);
}
public void RemoveMyFirstClass(MyFirstClass firstClass)
{
MyFirstClass.Remove -= firstClass_Changed;
firstClassList.Remove(firstClass);
}
}
Another option is to pass a callback function. You should avoid this, unless you need it explicity:
public class MyFirstClass
{
EventHandler handler;
public MyFirstClass(EventHandler handler)
{
this.handler = handler;
}
public void MyMethod()
{
handler?.Invoke(this, new EventArgs());
}
}
public class MySecondClass
{
private List<MyFirstClass> firstClassList;
public MySecondClass()
{
firstClassList = new List<MyFirstClass>();
}
// you have instantiated your class and passed your callback function previously.
public void AddMyFirstClass(MyFirstClass firstClass)
{
firstClassList.Add(firstClass);
}
// for demonstrating the instantiation.
public void AddMyFirstClass(EventHandler handler)
{
firstClassList.Add(new MyFirstClass(handler));
}
}

Event handler throwing object reference error

I am trying to create an event inside my class and handle it from static void main method.my event is triggered by a method named checkAge().But i have got an error like this :
Error1-An object reference is required for the non-static field,
method, or property 'Event.Program.m_AgeChecker(int)
I think i did all prats that i had to do,& i don't know what is the problem.
Code of my first class
class Mahmud
{
public Mahmud()
{
name = "mahmud";
age = 25;
}
private string name;
private int age;
public string Name
{
get{return name;}
set{name=value;}
}
public int Age
{
get { return age; }
set { age = value; }
}
public void checkAge()
{
AgeUpdate(age);
}
public delegate void AgeEventHandler(int mAge);
public event AgeEventHandler AgeUpdate;
}
Code of the second class
static void Main(string[] args)
{
Mahmud m = new Mahmud();
m.AgeUpdate += new Event.Mahmud.AgeEventHandler(m_AgeChecker(m.Age));
m.Age = 16;
m.checkAge();
m.Age = 27;
m.checkAge();
}
private void m_AgeChecker(int A)
{
if (A > 25)
{
Console.WriteLine("!");
}
else
{
Console.WriteLine("ok");
}
}
It looks like you are calling a non-static method from a static method. You will need to make the method static.
static void m_AgeChecker(int A)
{
if (A > 25)
{
Console.WriteLine("!");
}
else
{
Console.WriteLine("ok");
}
}

How to forward asynchronous events to parent classes?

I'm unsure as to what is the best approach for passing events down the line to parent classes and in need of some feedback.
The example code below tries to illustrate what I want to achieve.
namespace test {
public delegate void TestCompletedEventHandler(object sender,
TestCompletedEventArgs e);
public class Manager {
CarList m_carlist = null;
public CarList Cars {
get { return m_carlist; }
set { m_carlist = value; }
}
public Manager() {
Cars = new CarList(this);
}
public void Report(bool successfull) {
//...
}
}
public class CarList : List<Car> {
protected internal event TestCompletedEventHandler
Car_TestCompleted = null;
protected readonly Manager m_manager = null;
public Manager Manager {
get { return m_manager; }
}
public CarList(Manager manager) {
m_manager = manager;
}
public void Test() {
foreach(Car car in this) {
bool ret = car.Test();
manager.Report(ret);
}
}
public void Add(Car car) {
//Is this a good approach?
car.TestCompleted +=
new TestCompletedEventHandler(Car_TestCompleted_Method);
base.Add(car);
}
private void Car_TestCompleted_Method(object sender,
TestCompletedEventArgs e)
{
if(Car_TestCompleted != null) Car_TestCompleted(sender, e);
}
}
public class Car {
protected internal event TestCompletedEventHandler
TestCompleted = null;
public bool Test() {
//...
if(TestCompleted != null) TestCompleted(this,
new TestCompletedEventArgs())
}
}
public class TestCompletedEventArgs : EventArgs {
//...
}
}
using test;
Manager manager = new Manager();
manager.Cars.Car_TestCompleted +=
new TestCompletedEventHandler (Car_TestCompleted_Method);
manager.Cars.Test();
Another more specific example:
//Contains DataItems and interfaces for working with them
class DataList
{
public List<DataItem> m_dataitems { get; set; }
public TestManager m_testmanager { get; set; }
// ...
}
class DataItem
{
// ...
}
//A manager class for running tests on a DataList
class TestManager
{
public List<TestSource> m_sources { get; set; }
public WorkerManager m_workermanager { get; set; }
// ...
}
//A common interface for Tests
abstract class TestSource
{
public event EventHandler<EventArgs<object>> Completed = null;
protected TestManager m_owner { get; set; }
public abstract void RunAsync();
// ...
}
//A test
class Test1 : TestSource
{
public virtual void RunAsync()
{
//Add commands
//Run workers
//Report progress to DataList and other listeners (like UI)
//Events seem like a bad approach since they need to be forwarded through many levels of abstraction
if(Completed != null) Completed(this, new EventArgs<object>(null));
}
// ...
}
//Manages a number of workers and a queue of commands
class WorkerManager
{
public List<MyWorker> m_workers { get; set; }
public Queue<Command> m_commands { get; set; }
}
//Wrapper for BackgroundWorker
class MyWorker
{
// ...
}
//Async command
interface Command
{
// ...
}
I think you may have just over implemented this a bit... It looks like you are trying to use async operations. Even if you are using sync operations though, typically you'd just use callback methods instead of events in a case like this...
Here is an example of things to change to use callbacks here:
//new delegate
public delegate void CarReportCallback(Car theCar, bool result);
//in the Manager class, make report conform to delegate's signature
public void Report(Car theCar, bool result)
{
//do something, you know which car and what the result is.
}
//in the CarList class pass a reference to the report method in
public void Test()
{
foreach(Car car in this)
{
car.Test(manager.Report);
}
}
//in the Car class use the delegate passed to invoke the reporting
public void Test(CarReportCallback callback)
{
//... do stuff
callback(this, isTestCompleted);
}
It seems reasonable, but I'm not really sure what the use case is and how this would be used.
You've got a strong concept of containment going on, but I'm not really sure why. Also, it's kind of weird that the CarList 'sort of' seems to have ownership of the individual cars.
Additionally, I don't know why Test() on the Car class would both return a result and raise an event. It seems like you're having two different paths to return the same data. And the Manager class seems completely redundant with the CarList class at first glance.
What is the problem you're actually trying to solve here? That might help me with defining a good solution to it.
It wouldn't make sense to just have each car call an event which calls an event on the parent list. I would do it more like this:
namespace test {
public delegate void TestCompletedEventHandler(object sender,
TestCompletedEventArgs e);
public class Manager {
CarList m_carlist = null;
public CarList Cars {
get { return m_carlist; }
set { m_carlist = value; }
}
public Manager() {
Cars = new CarList(this);
}
public void Report(bool successful) {
//...
}
}
public class CarList : List<Car> {
protected readonly Manager m_manager = null;
protected List<Action<object, TestCompletedEventArgs>> delegatesList = new List<Action<object, TestCompletedEventArgs>>();
public Manager Manager {
get { return m_manager; }
}
public CarList(Manager manager) {
m_manager = manager;
}
public void Test() {
foreach(Car car in this) {
bool ret = car.Test();
manager.Report(ret);
}
}
public void Add(TestCompletedEventHandler e) {
foreach (Car car in this) {
car.OnTestCompleted += e;
}
delegatesList.Add(e);
}
public void Add(Car car) {
foreach(Action a in delegatesList)
{
car.OnTestCompleted += a;
}
base.Add(car);
}
}
public class Car {
protected internal event TestCompletedEventHandler OnTestCompleted = null;
public bool Test() {
//...
if (OnTestCompleted != null) OnTestCompleted(this, new TestCompletedEventArgs());
}
}
public class TestCompletedEventArgs : EventArgs {
//...
}
}
using test;
Manager manager = new Manager();
Manager.Cars.Add(new Car());
manager.Cars.Add(new Car());
manager.Cars.Add(new Car());
manager.Cars.Add((sender, args) =>
{
//do whatever...
})
manager.Cars.Test();
manager.Cars.Add(new Car());

Categories