function ptr

Hi,

i want a function pointer in project
i think delegate/events can be used in place
can any one help with some e.g


Answer this question

function ptr

  • ParkSys

    This is a Sample according to publisher-subscriber scenario in Observer design pattern which will give you the idea of creating and using the custom delegate - which is the same concept .Net for Function pointer

    On publisher side:
    Delegate:

    public void delegate MyDelegate(MyType sender, MyParamCollection params);

     


    Event in MyType:

    public event MyDelegate MyEvent;

     
    define some method for raising event like,


    private void OnMyEvent() {
        MyParamCollection coll = new MyParamCollection();
        :
        :
        if(MyEvent !=null) {
            MyEvent(this, coll);
        }
    }

     

    and call this method at some point where you want to raise the specific event,
     
    On subscriber side:


    objMyType.MyEvent += new MyDelegate(MyEventHandlerMethod);
    private void MyEventHandlerMethod(MyType sender, MyParamCollection params) {
        do something.....
    }

     


    HTH,

  • Rajavanya

    I think it's delegate. I copied this from .NET framework SDK Doc

    The delegate keyword is used to declare a reference type that can be used to encapsulate a named or an anonymous method. Delegates are similar to function pointers in C++; however, delegates are type-safe and secure

    and Event in .NET utilize this delegate things

    check this

    http://www.codeproject.com/csharp/delegates-part1.asp



  • function ptr