R
raylopez99
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/library/8627sbea(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()...nothing
The End
Press any key to continue . . .
///////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
class Program
{
static void Main(string[] args)
{
// http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx
PropertyEventsSample myPES = new PropertyEventsSample();
// manual way - works but not automated
MyDelegate1 x = null;
// x += new MyDelegate1(myPES.myTest1.myFoo1); //old style
x += myPES.myTest1.myFoo1;
x(1); //works
x += new MyDelegate1(myPES.myTest1.myFoo12);
x(10); //works
MyDelegate2 x2 = null;
x2 += new MyDelegate2(myPES.myTest1.myFoo2);
x2("Ray");
MyDelegate3 x3 = null;
x3 += new MyDelegate3(myPES.myTest1.myFoo3);
x3(123, '!');
x3 += new MyDelegate3(myPES.myTest1.myFoo32);
x3(123, '$');
Console.WriteLine("notice chaining of delegates for x3");
MyDelegate4 x4 = null;
x4 += new MyDelegate4(myPES.myTest1.myFoo4);
x4();
// trying to get this to fire...failed
//myPES.Event1 += new MyDelegate1(myPES.myTest1.myFoo1);
//myPES.Event2 += new MyDelegate1(myPES.myTest1.myFoo12);
////
//myPES.Event3 += new MyDelegate2(myPES.myTest1.myFoo2);
////
//myPES.Event4 += new MyDelegate3(myPES.myTest1.myFoo3);
//myPES.Event5 += new MyDelegate3(myPES.myTest1.myFoo32);
////
//myPES.Event6 += new MyDelegate4(myPES.myTest1.myFoo4);
//////////////////////////// failed///
// myPES.FireAway("Event1");
// myPES.Event1(1);
// myPES.Event1.Invoke(1);
// myPES.eventTable["Event1"]
Console.WriteLine("The End");
}
}
}
//////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(string 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.WriteLine("hi:{0}", i); }
public void myFoo12(int i)
{ this.i = 2*i; }
public void myFoo2(string s)
{ this.s = s; Console.WriteLine(s); }
public void myFoo3(int i, object o)
{ Console.WriteLine("i, object are: {0}, {1}", i, o); }
public void myFoo32(int i, object o)
{ Console.WriteLine("i, object areTWO: {0}, {1}", i, o); }
public void myFoo4()
{ Console.WriteLine("myFoo4()...nothing"); }
}
public class PropertyEventsSample
{
//http://msdn.microsoft.com/en-us/library/8627sbea(VS.
71).aspx
public Test1 myTest1;
private Hashtable eventTable;
public PropertyEventsSample()
{
myTest1 = new Test1();
eventTable = new Hashtable();
Event1 += new MyDelegate1(myTest1.myFoo1);
// eventTable.Add("Event1", Event1); //improper, does not work
//eventTable.Add("Event1", Event1); //improper, does not work
//error CS0079 The event
'MediatorC.PropertyEventsSample.Event1' can only appear on the left
hand side of += or -=
Event2 += new MyDelegate1(myTest1.myFoo12);
//
Event3 += new MyDelegate2(myTest1.myFoo2);
//
Event4 += new MyDelegate3(myTest1.myFoo3);
Event5 += new MyDelegate3(myTest1.myFoo32);
//
Event6 += new MyDelegate4(myTest1.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)eventTable["Event1"] + value;
}
remove
{
eventTable["Event1"] =
(MyDelegate1)eventTable["Event1"] - value;
}
}
public event MyDelegate1 Event2
{
add
{
eventTable["Event2"] =
(MyDelegate1)eventTable["Event2"] + value;
}
remove
{
eventTable["Event2"] =
(MyDelegate1)eventTable["Event2"] - value;
}
}
public event MyDelegate2 Event3
{
add
{
eventTable["Event3"] =
(MyDelegate2)eventTable["Event3"] + value;
}
remove
{
eventTable["Event3"] =
(MyDelegate2)eventTable["Event3"] - value;
}
}
public event MyDelegate3 Event4
{
add
{
eventTable["Event4"] =
(MyDelegate3)eventTable["Event4"] + value;
}
remove
{
eventTable["Event4"] =
(MyDelegate3)eventTable["Event4"] - value;
}
}
public event MyDelegate3 Event5
{
add
{
eventTable["Event5"] =
(MyDelegate3)eventTable["Event5"] + value;
}
remove
{
eventTable["Event5"] =
(MyDelegate3)eventTable["Event5"] - value;
}
}
public event MyDelegate4 Event6
{
add
{
eventTable["Event6"] =
(MyDelegate4)eventTable["Event6"] + value;
}
remove
{
eventTable["Event6"] =
(MyDelegate4)eventTable["Event6"] - value;
}
}
}
}
///////////////////////////////////////
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/library/8627sbea(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()...nothing
The End
Press any key to continue . . .
///////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
class Program
{
static void Main(string[] args)
{
// http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx
PropertyEventsSample myPES = new PropertyEventsSample();
// manual way - works but not automated
MyDelegate1 x = null;
// x += new MyDelegate1(myPES.myTest1.myFoo1); //old style
x += myPES.myTest1.myFoo1;
x(1); //works
x += new MyDelegate1(myPES.myTest1.myFoo12);
x(10); //works
MyDelegate2 x2 = null;
x2 += new MyDelegate2(myPES.myTest1.myFoo2);
x2("Ray");
MyDelegate3 x3 = null;
x3 += new MyDelegate3(myPES.myTest1.myFoo3);
x3(123, '!');
x3 += new MyDelegate3(myPES.myTest1.myFoo32);
x3(123, '$');
Console.WriteLine("notice chaining of delegates for x3");
MyDelegate4 x4 = null;
x4 += new MyDelegate4(myPES.myTest1.myFoo4);
x4();
// trying to get this to fire...failed
//myPES.Event1 += new MyDelegate1(myPES.myTest1.myFoo1);
//myPES.Event2 += new MyDelegate1(myPES.myTest1.myFoo12);
////
//myPES.Event3 += new MyDelegate2(myPES.myTest1.myFoo2);
////
//myPES.Event4 += new MyDelegate3(myPES.myTest1.myFoo3);
//myPES.Event5 += new MyDelegate3(myPES.myTest1.myFoo32);
////
//myPES.Event6 += new MyDelegate4(myPES.myTest1.myFoo4);
//////////////////////////// failed///
// myPES.FireAway("Event1");
// myPES.Event1(1);
// myPES.Event1.Invoke(1);
// myPES.eventTable["Event1"]
Console.WriteLine("The End");
}
}
}
//////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MediatorC
{
public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(string 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.WriteLine("hi:{0}", i); }
public void myFoo12(int i)
{ this.i = 2*i; }
public void myFoo2(string s)
{ this.s = s; Console.WriteLine(s); }
public void myFoo3(int i, object o)
{ Console.WriteLine("i, object are: {0}, {1}", i, o); }
public void myFoo32(int i, object o)
{ Console.WriteLine("i, object areTWO: {0}, {1}", i, o); }
public void myFoo4()
{ Console.WriteLine("myFoo4()...nothing"); }
}
public class PropertyEventsSample
{
//http://msdn.microsoft.com/en-us/library/8627sbea(VS.
71).aspx
public Test1 myTest1;
private Hashtable eventTable;
public PropertyEventsSample()
{
myTest1 = new Test1();
eventTable = new Hashtable();
Event1 += new MyDelegate1(myTest1.myFoo1);
// eventTable.Add("Event1", Event1); //improper, does not work
//eventTable.Add("Event1", Event1); //improper, does not work
//error CS0079 The event
'MediatorC.PropertyEventsSample.Event1' can only appear on the left
hand side of += or -=
Event2 += new MyDelegate1(myTest1.myFoo12);
//
Event3 += new MyDelegate2(myTest1.myFoo2);
//
Event4 += new MyDelegate3(myTest1.myFoo3);
Event5 += new MyDelegate3(myTest1.myFoo32);
//
Event6 += new MyDelegate4(myTest1.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)eventTable["Event1"] + value;
}
remove
{
eventTable["Event1"] =
(MyDelegate1)eventTable["Event1"] - value;
}
}
public event MyDelegate1 Event2
{
add
{
eventTable["Event2"] =
(MyDelegate1)eventTable["Event2"] + value;
}
remove
{
eventTable["Event2"] =
(MyDelegate1)eventTable["Event2"] - value;
}
}
public event MyDelegate2 Event3
{
add
{
eventTable["Event3"] =
(MyDelegate2)eventTable["Event3"] + value;
}
remove
{
eventTable["Event3"] =
(MyDelegate2)eventTable["Event3"] - value;
}
}
public event MyDelegate3 Event4
{
add
{
eventTable["Event4"] =
(MyDelegate3)eventTable["Event4"] + value;
}
remove
{
eventTable["Event4"] =
(MyDelegate3)eventTable["Event4"] - value;
}
}
public event MyDelegate3 Event5
{
add
{
eventTable["Event5"] =
(MyDelegate3)eventTable["Event5"] + value;
}
remove
{
eventTable["Event5"] =
(MyDelegate3)eventTable["Event5"] - value;
}
}
public event MyDelegate4 Event6
{
add
{
eventTable["Event6"] =
(MyDelegate4)eventTable["Event6"] + value;
}
remove
{
eventTable["Event6"] =
(MyDelegate4)eventTable["Event6"] - value;
}
}
}
}
///////////////////////////////////////