Sender object in event?

A

Asaf

Hi,

I have an OCX DLL that I am referencing to it in my Windows Service
application.
As I am using this OCX as an array of objects and generating event for it I
would like to know what is the Index of the object from the array that called
the event.

Unfortunately when creating the event from the object it does not generating
the (object sender…) so I can’t loop for the sender and equal it to array to
check his index.

Is there a way to add the sender object to the event?

Thanks in advanced for any help,
Asaf
 
A

Alberto Poblacion

Asaf said:
I have an OCX DLL that I am referencing to it in my Windows Service
application.
As I am using this OCX as an array of objects and generating event for it
I
would like to know what is the Index of the object from the array that
called
the event.

Unfortunately when creating the event from the object it does not
generating
the (object sender…) so I can’t loop for the sender and equal it to array
to
check his index.

Is there a way to add the sender object to the event?


Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class could
connect the event of the encapsulated OCX into your own event (adding the
"sender"), and then your own event would be the one that you would use from
outside the class.
If you only need the index, instead of looping through the array you
could add it as an additional property of your class and initialize it while
building the array:

public delegate void MyEventHandler(Object sender, TheArgsOfTheOcxEvent e);

public class MyOCX
{
public int Index;
public event MyEventHandler MyOCXEvent;
public TheType TheOCX;

public MyOCX(int index, int theOCX)
{
this.Index = index;
this.TheOCX = theOCX;
theOCX.TheEvent += RelayEvent;
}

private void RelayEvent(TheArgsOfTheOcxEvent e)
{
if (MyOCXEvent!=null)
MyOCXEvent(this, e);
}
}

//------------

//Build the array:

MyOCX[] theArray = new MyOCX[n];

for (int i=0; i<n; i++)
{
TheType theOCX = Activator.CreateInstance(...); //Or whatever
MyOCX entry = new MyOCX(i, theOCX);
entry.MyOCXEvent += MyEventProc;
theArray = entry;
}

//Process the events:

private void MyEventProc(object sender, TheArgsOfTheOcxEvent e)
{
MyOCX entry = (MyOCX)sender;
int index = entry.Index;
//Do whatever with index
}
 
A

Alberto Poblacion

Asaf said:
I have an OCX DLL that I am referencing to it in my Windows Service
application.
As I am using this OCX as an array of objects and generating event for it
I
would like to know what is the Index of the object from the array that
called
the event.

Unfortunately when creating the event from the object it does not
generating
the (object sender…) so I can’t loop for the sender and equal it to array
to
check his index.

Is there a way to add the sender object to the event?


Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class could
connect the event of the encapsulated OCX into your own event (adding the
"sender"), and then your own event would be the one that you would use from
outside the class.
If you only need the index, instead of looping through the array you
could add it as an additional property of your class and initialize it while
building the array:

public delegate void MyEventHandler(Object sender, TheArgsOfTheOcxEvent e);

public class MyOCX
{
public int Index;
public event MyEventHandler MyOCXEvent;
public TheType TheOCX;

public MyOCX(int index, int theOCX)
{
this.Index = index;
this.TheOCX = theOCX;
theOCX.TheEvent += RelayEvent;
}

private void RelayEvent(TheArgsOfTheOcxEvent e)
{
if (MyOCXEvent!=null)
MyOCXEvent(this, e);
}
}

//------------

//Build the array:

MyOCX[] theArray = new MyOCX[n];

for (int i=0; i<n; i++)
{
TheType theOCX = Activator.CreateInstance(...); //Or whatever
MyOCX entry = new MyOCX(i, theOCX);
entry.MyOCXEvent += MyEventProc;
theArray = entry;
}

//Process the events:

private void MyEventProc(object sender, TheArgsOfTheOcxEvent e)
{
MyOCX entry = (MyOCX)sender;
int index = entry.Index;
//Do whatever with index
}
 
A

Asaf

Hello Alberto,

Many thanks for your help!

Regards,
Asaf

Alberto Poblacion said:
Asaf said:
I have an OCX DLL that I am referencing to it in my Windows Service
application.
As I am using this OCX as an array of objects and generating event for it
I
would like to know what is the Index of the object from the array that
called
the event.

Unfortunately when creating the event from the object it does not
generating
the (object sender…) so I can’t loop for the sender and equal it to array
to
check his index.

Is there a way to add the sender object to the event?


Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class could
connect the event of the encapsulated OCX into your own event (adding the
"sender"), and then your own event would be the one that you would use from
outside the class.
If you only need the index, instead of looping through the array you
could add it as an additional property of your class and initialize it while
building the array:

public delegate void MyEventHandler(Object sender, TheArgsOfTheOcxEvent e);

public class MyOCX
{
public int Index;
public event MyEventHandler MyOCXEvent;
public TheType TheOCX;

public MyOCX(int index, int theOCX)
{
this.Index = index;
this.TheOCX = theOCX;
theOCX.TheEvent += RelayEvent;
}

private void RelayEvent(TheArgsOfTheOcxEvent e)
{
if (MyOCXEvent!=null)
MyOCXEvent(this, e);
}
}

//------------

//Build the array:

MyOCX[] theArray = new MyOCX[n];

for (int i=0; i<n; i++)
{
TheType theOCX = Activator.CreateInstance(...); //Or whatever
MyOCX entry = new MyOCX(i, theOCX);
entry.MyOCXEvent += MyEventProc;
theArray = entry;
}

//Process the events:

private void MyEventProc(object sender, TheArgsOfTheOcxEvent e)
{
MyOCX entry = (MyOCX)sender;
int index = entry.Index;
//Do whatever with index
}
 
A

Asaf

Hello Alberto,

Many thanks for your help!

Regards,
Asaf

Alberto Poblacion said:
Asaf said:
I have an OCX DLL that I am referencing to it in my Windows Service
application.
As I am using this OCX as an array of objects and generating event for it
I
would like to know what is the Index of the object from the array that
called
the event.

Unfortunately when creating the event from the object it does not
generating
the (object sender…) so I can’t loop for the sender and equal it to array
to
check his index.

Is there a way to add the sender object to the event?


Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class could
connect the event of the encapsulated OCX into your own event (adding the
"sender"), and then your own event would be the one that you would use from
outside the class.
If you only need the index, instead of looping through the array you
could add it as an additional property of your class and initialize it while
building the array:

public delegate void MyEventHandler(Object sender, TheArgsOfTheOcxEvent e);

public class MyOCX
{
public int Index;
public event MyEventHandler MyOCXEvent;
public TheType TheOCX;

public MyOCX(int index, int theOCX)
{
this.Index = index;
this.TheOCX = theOCX;
theOCX.TheEvent += RelayEvent;
}

private void RelayEvent(TheArgsOfTheOcxEvent e)
{
if (MyOCXEvent!=null)
MyOCXEvent(this, e);
}
}

//------------

//Build the array:

MyOCX[] theArray = new MyOCX[n];

for (int i=0; i<n; i++)
{
TheType theOCX = Activator.CreateInstance(...); //Or whatever
MyOCX entry = new MyOCX(i, theOCX);
entry.MyOCXEvent += MyEventProc;
theArray = entry;
}

//Process the events:

private void MyEventProc(object sender, TheArgsOfTheOcxEvent e)
{
MyOCX entry = (MyOCX)sender;
int index = entry.Index;
//Do whatever with index
}
 
B

Ben Voigt [C++ MVP]

Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 
B

Ben Voigt [C++ MVP]

Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 
A

Asaf

Hello Ben,

Thanks for your reply, but I didn’t figure how to use your example so I am
posting my code here if you can help me please.

Here is the code:

private void InitSMPPEvents()
{
for (int i = 0; i <= objSMPP.GetUpperBound(0); i++)
{
objSMPP = new smscr.SMSCRelaySMPP();
objSMPP.Disconnected += new
smscr.ISMSCRelaySMPPEvents_DisconnectedEventHandler(SilverSMS_MSMQ_Disconnected);
}
}

void SilverSMS_MSMQ_Disconnected(int Reason)
{
throw new NotImplementedException();
}


Problem is that when generating "Disconnected" event there is only "int
Reason" without the "object sender" so I can’t know who is the object from
the array that triggered the event "SilverSMS_MSMQ_Disconnected".

Will be much appreciated if you can show me how to implement your example on
my code.

Thanks,
Asaf


Ben Voigt said:
Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 
A

Asaf

Hello Ben,

Thanks for your reply, but I didn’t figure how to use your example so I am
posting my code here if you can help me please.

Here is the code:

private void InitSMPPEvents()
{
for (int i = 0; i <= objSMPP.GetUpperBound(0); i++)
{
objSMPP = new smscr.SMSCRelaySMPP();
objSMPP.Disconnected += new
smscr.ISMSCRelaySMPPEvents_DisconnectedEventHandler(SilverSMS_MSMQ_Disconnected);
}
}

void SilverSMS_MSMQ_Disconnected(int Reason)
{
throw new NotImplementedException();
}


Problem is that when generating "Disconnected" event there is only "int
Reason" without the "object sender" so I can’t know who is the object from
the array that triggered the event "SilverSMS_MSMQ_Disconnected".

Will be much appreciated if you can show me how to implement your example on
my code.

Thanks,
Asaf


Ben Voigt said:
Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 
A

Asaf

Hello Ben,

I have managed to get your code working on my project.
Many thanks for solving my problem!

Regards,
Asaf

Ben Voigt said:
Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 
A

Asaf

Hello Ben,

I have managed to get your code working on my project.
Many thanks for solving my problem!

Regards,
Asaf

Ben Voigt said:
Alberto Poblacion said:
Instead of building an array of the OCX, you could build an array of
your own class, which would in turn encapsulate the OCX plus an event
defined by you (including the sender). The constructor for your class
could connect the event of the encapsulated OCX into your own event
(adding the "sender"), and then your own event would be the one that you
would use from outside the class.

You can let the C# compiler do most of the work for you:

for( int i = 0; i < ocxs.Length; i++ ) {
int captureIndex = i;
var captureOCX = ocxs;
captureOCX.SomeEvent += args => Handler(captureIndex, captureOCX, args);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top