Book a Demo

Author Topic: Use function prototypes (C#)  (Read 3569 times)

Stringer

  • EA User
  • **
  • Posts: 37
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Use function prototypes (C#)
« on: September 11, 2007, 12:11:05 pm »
I have a event in my class. Can I reuse that event in another class and make a Operation based on that event? I mean automaticly without rewriting it.

Example source:

public delegate void EventPrototype(object sender, byte[] data);

public interface IMyInterface {
  event EventPrototype ReceivedData;
}

// Implementing the event
class MyEventClass : IMyInterface
{
 public event EventPrototype ReceivedData;

 protected void OnReceiveData(object sender, byte[] data)
 {
   if (ReceivedData != null)
     ReceivedData(sender, data);
 }
}

// Receiving the event
public class B
{
 public MyEventClass Client=new MyEventClass();

 public B()
 {
     Client.ReceivedData += new EventPrototype(Receive);
 }

 /* Inside the comments is the Operation I would like to have derived automaticly from the EventPrototype
 public void Receive(object sender, byte[] data)
 {
 }*/

}
« Last Edit: September 11, 2007, 12:19:42 pm by Stringer »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Use function prototypes (C#)
« Reply #1 on: September 11, 2007, 01:17:01 pm »
Try this thought experiment...

If you were just building the software - not modeling it - how would you approach reusing the event? Would you describe it as a feature of the class, and expose it to collaborating classes? Or would you create it as an entity on its own, then reference it from any given 'owning' class? Or would you use some other method? You get the idea.

Now back to modeling. How would you represent your chosen paradigm?

Think of assigning a stereotype or tagged values to the event, depending on which way you approach this, and what EA offers you.

Here's a hint. Whip up some very simple code that does what you want. Reverse engineer it into EA. Now take a look at how EA handles the event.

Let us know if this gets you unstuck.

David
No, you can't have it!

Stringer

  • EA User
  • **
  • Posts: 37
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Use function prototypes (C#)
« Reply #2 on: September 12, 2007, 02:36:34 am »
Yea, but I wanted a automated way to add the event receiver to some class. The automation would depend on the function prototype (delegate) defined for the event in the (event)source class. Now I can always do that by adding a new  Receive() function to Operations and then adding parameters to it, but to automate that?

Example of the simple code I use in the C# for events was in my first post.

How would stereotypes or tagged values help with this? Actually I didn't find much help for the very purpose of stereotypes at all from the EA Help. I mean other purpose than some appearance settings.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Use function prototypes (C#)
« Reply #3 on: September 12, 2007, 02:49:18 am »
Did you try reverse engineering the code into EA? If so, what did EA do with your prototype?
No, you can't have it!

Stringer

  • EA User
  • **
  • Posts: 37
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Use function prototypes (C#)
« Reply #4 on: September 12, 2007, 03:08:23 am »
I think it didn't do anything with the prototype itself, because it is defined at the namespace rather than in the class/interface. If I remove the separate interface and move the delegate directly to the MyEventClass, it will show the delegate as a Operation in the corresponding class. But that is not the point now, or is it?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Use function prototypes (C#)
« Reply #5 on: September 12, 2007, 03:46:54 am »
My point is to examine how EA describes the delegate, then see if you can leverage that pattern elsewhere in your models to get the output you need.
No, you can't have it!