Hello all—
I’m trying to get the below to work and cannot get the format right.
It’s from this example: http://msdn.microsoft.com/en-us/libr...ea(VS.71).aspx
What it is: I’m trying to store multicast delegates in a hash table,
and then fire the delegates one of two ways (after registering/
creating the delegates, etc).
First, either traversing the entire hash table, using foreach
(Hashtable h in eventTable) logic (see- //does not work-how to make it
work?).
Second, by calling up the value of a hashtable through the key. But
how to cast and fire the delegate? I get the error CS0079, see below.
But manually I can get these delegates to fire (see output at Main
below). However, I want to automate this process (I think that’s the
point behind the link above, though I could be wrong).
Any suggestions even outside of actual code appreciated.
RL
/////////////// works, but not ‘automated’
hi:1
hi:10
Ray
i, object are: 123, !
i, object are: 123, $
i, object areTWO: 123, $
notice chaining of delegates for x3
myFoo4()...noth ing
The End
Press any key to continue . . .
///////////////
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
class Program
{
static void Main(string[] args)
{
// http://msdn.microsoft.com/en-us/libr...ea(VS.71).aspx
PropertyEventsS ample myPES = new PropertyEventsS ample();
// manual way - works but not automated
MyDelegate1 x = null;
// x += new MyDelegate1(myP ES.myTest1.myFo o1); //old style
x += myPES.myTest1.m yFoo1;
x(1); //works
x += new MyDelegate1(myP ES.myTest1.myFo o12);
x(10); //works
MyDelegate2 x2 = null;
x2 += new MyDelegate2(myP ES.myTest1.myFo o2);
x2("Ray");
MyDelegate3 x3 = null;
x3 += new MyDelegate3(myP ES.myTest1.myFo o3);
x3(123, '!');
x3 += new MyDelegate3(myP ES.myTest1.myFo o32);
x3(123, '$');
Console.WriteLi ne("notice chaining of delegates for x3");
MyDelegate4 x4 = null;
x4 += new MyDelegate4(myP ES.myTest1.myFo o4);
x4();
// trying to get this to fire...failed
//myPES.Event1 += new MyDelegate1(myP ES.myTest1.myFo o1);
//myPES.Event2 += new MyDelegate1(myP ES.myTest1.myFo o12);
////
//myPES.Event3 += new MyDelegate2(myP ES.myTest1.myFo o2);
////
//myPES.Event4 += new MyDelegate3(myP ES.myTest1.myFo o3);
//myPES.Event5 += new MyDelegate3(myP ES.myTest1.myFo o32);
////
//myPES.Event6 += new MyDelegate4(myP ES.myTest1.myFo o4);
//////////////////////////// failed///
// myPES.FireAway( "Event1");
// myPES.Event1(1) ;
// myPES.Event1.In voke(1);
// myPES.eventTabl e["Event1"]
Console.WriteLi ne("The End");
}
}
}
//////////////////////////////////////////////////
using System;
using System.Collecti ons;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(str ing s);
public delegate void MyDelegate3(int i, object o);
public delegate void MyDelegate4();
public class Test1
{
public int i;
public string s;
public Test1()
{ i = 0; s = "hi!"; }
public void myFoo1(int i)
{ this.i = i; Console.WriteLi ne("hi:{0}", i); }
public void myFoo12(int i)
{ this.i = 2*i; }
public void myFoo2(string s)
{ this.s = s; Console.WriteLi ne(s); }
public void myFoo3(int i, object o)
{ Console.WriteLi ne("i, object are: {0}, {1}", i, o); }
public void myFoo32(int i, object o)
{ Console.WriteLi ne("i, object areTWO: {0}, {1}", i, o); }
public void myFoo4()
{ Console.WriteLi ne("myFoo4()... nothing"); }
}
public class PropertyEventsS ample
{
//http://msdn.microsoft. com/en-us/library/8627sbea(VS.
71).aspx
public Test1 myTest1;
private Hashtable eventTable;
public PropertyEventsS ample()
{
myTest1 = new Test1();
eventTable = new Hashtable();
Event1 += new MyDelegate1(myT est1.myFoo1);
// eventTable.Add( "Event1", Event1); //improper, does not work
//eventTable.Add( "Event1", Event1); //improper, does not work
//error CS0079 The event
'MediatorC.Prop ertyEventsSampl e.Event1' can only appear on the left
hand side of += or -=
Event2 += new MyDelegate1(myT est1.myFoo12);
//
Event3 += new MyDelegate2(myT est1.myFoo2);
//
Event4 += new MyDelegate3(myT est1.myFoo3);
Event5 += new MyDelegate3(myT est1.myFoo32);
//
Event6 += new MyDelegate4(myT est1.myFoo4);
}
public void FireAway()
{
//if (eventTable[str1] != null)
//{ }
foreach (Hashtable h in eventTable)
{
//h.Values; //does not work-how to make it work?
}
//
}
public event MyDelegate1 Event1
{
add
{
eventTable["Event1"] =
(MyDelegate1)ev entTable["Event1"] + value;
}
remove
{
eventTable["Event1"] =
(MyDelegate1)ev entTable["Event1"] - value;
}
}
public event MyDelegate1 Event2
{
add
{
eventTable["Event2"] =
(MyDelegate1)ev entTable["Event2"] + value;
}
remove
{
eventTable["Event2"] =
(MyDelegate1)ev entTable["Event2"] - value;
}
}
public event MyDelegate2 Event3
{
add
{
eventTable["Event3"] =
(MyDelegate2)ev entTable["Event3"] + value;
}
remove
{
eventTable["Event3"] =
(MyDelegate2)ev entTable["Event3"] - value;
}
}
public event MyDelegate3 Event4
{
add
{
eventTable["Event4"] =
(MyDelegate3)ev entTable["Event4"] + value;
}
remove
{
eventTable["Event4"] =
(MyDelegate3)ev entTable["Event4"] - value;
}
}
public event MyDelegate3 Event5
{
add
{
eventTable["Event5"] =
(MyDelegate3)ev entTable["Event5"] + value;
}
remove
{
eventTable["Event5"] =
(MyDelegate3)ev entTable["Event5"] - value;
}
}
public event MyDelegate4 Event6
{
add
{
eventTable["Event6"] =
(MyDelegate4)ev entTable["Event6"] + value;
}
remove
{
eventTable["Event6"] =
(MyDelegate4)ev entTable["Event6"] - value;
}
}
}
}
///////////////////////////////////////