how to point to a set of parameters using delegate

Hello,
I wonder if it's possible to point to a set of parameters or values when calling a function with a delegate. I have 2 forms and I pass delegate to call functions from form1.
lets say on form2 i have:
public delegate bool CallFuncDelegate();
private int generalX, general Y;

public bool Call(delegate, int a, int b)
{ generalX = a;
generalY = b;
invoke();
}

public bool Func1()
{ int localX = generalX;
int localY = generalY;
....
}

public bool Func2()
{ int localX = generalX;
int localY = generalY;
....
}

On form1 i will assign Func() to the delegate and call it thru Call():
form2.CallFuncDelegate CFDelegate = new form2.CallFuncDelegate (form2.Func1);
AF.Logon(CFDelegate, 1, 2);

form2.CallFuncDelegate CFDelegate = new form2.CallFuncDelegate (form2.Func2);
AF.Logon(CFDelegate, 8, 9);

So is there a way to call Func() in form one and at the same time tell which parameters to use in form2
so it's gonna be like this on form2:
public delegate bool CallFuncDelegate();
setofParam1{int generalX =1; int generalY =2};
setofParam2{int generalX=8; int generalY=9};

public bool Call(delegate)
{
invoke();
}

public bool Func1()
{ int localX = setofParam1.generalX;
int localY = setofParam1.generalY;
....
}

public bool Func2()
{ int localX = setofParam2.generalX;
int localY = setofParam2.generalY;
....
}

On form1:
form2.CallFuncDelegate CFDelegate = new form2.CallFuncDelegate (form2.Func1);
AF.Logon(CFDelegate); // and then activate set1

form2.CallFuncDelegate CFDelegate = new form2.CallFuncDelegate (form2.Func2);
AF.Logon(CFDelegate);// and then activate set2

Sorry for being a little bit confusing. I want to do that because sometimes Func() can have less or more parameters and that required me to change the Call() and it's signature.



Answer this question

how to point to a set of parameters using delegate

  • Hemant Kanoujiya_29

    so example or codes may be very helpful
    thx

  • Jamel

    Ahh, if only the rest of the BCL and framework provided no interfaces that promoted bad programming practices.

  • Ashok_Roy

    The docs for EventArgs provide a reasonable example:


    using System;
    // FireEventArgs: a custom event inherited from EventArgs.
    public class FireEventArgs: EventArgs {
    public FireEventArgs(string room, int ferocity) {
    this.room = room;
    this.ferocity = ferocity;
    }
    // The fire event will have two pieces of information--
    // 1) Where the fire is, and 2) how "ferocious" it is.
    public string room;
    public int ferocity;
    } //end of class FireEventArgs

    // Class with a function that creates the eventargs and initiates the event
    public class FireAlarm {
    // Events are handled with delegates, so we must establish a FireEventHandler
    // as a delegate:
    public delegate void FireEventHandler(object sender, FireEventArgs fe);
    // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.
    public event FireEventHandler FireEvent;
    // This will be the starting point of our event-- it will create FireEventArgs,
    // and then raise the event, passing FireEventArgs.
    public void ActivateFireAlarm(string room, int ferocity) {
    FireEventArgs fireArgs = new FireEventArgs(room, ferocity);
    // Now, raise the event by invoking the delegate. Pass in
    // the object that initated the event (this) as well as FireEventArgs.
    // The call must match the signature of FireEventHandler.
    FireEvent(this, fireArgs);
    }
    } // end of class FireAlarm

    // Class which handles the event
    class FireHandlerClass {
    // Create a FireAlarm to handle and raise the fire events.
    public FireHandlerClass(FireAlarm fireAlarm) {
    // Add a delegate containing the ExtinguishFire function to the class'
    // event so that when FireAlarm is raised, it will subsequently execute
    // ExtinguishFire.
    fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
    }
    // This is the function to be executed when a fire event is raised.

    void ExtinguishFire(object sender, FireEventArgs fe) {
    Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());
    // Now, act in response to the event.
    if (fe.ferocity < 2)
    Console.WriteLine("This fire in the {0} is no problem. I'm going to pour some water on it.", fe.room);
    else if (fe.ferocity < 5)
    Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.", fe.room);
    else
    Console.WriteLine("The fire in the {0} is out of control. I'm calling the fire department!", fe.room);
    }
    } //end of class FireHandlerClass
    public class FireEventTest {
    public static void Main () {
    // Create an instance of the class that will be firing an event.
    FireAlarm myFireAlarm = new FireAlarm();

    // Create an instance of the class that will be handling the event. Note that
    // it receives the class that will fire the event as a parameter.
    FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);

    //use our class to raise a few events and watch them get handled
    myFireAlarm.ActivateFireAlarm("Kitchen", 3);
    myFireAlarm.ActivateFireAlarm("Study", 1);
    myFireAlarm.ActivateFireAlarm("Porch", 5);

    return;
    } //end of main
    } // end of FireEventTest



  • 六子

    That probably would have promoted really really bad programming practices :D

  • Jens Leidel

    Way too complicated for a small functionality.

  • TWeiler

    It would have been nice to have a Tag property on EventArgs--that way an event using the built-in EventArgs could simply pass an array of arguments as the Tag property.

    But, alas, that is not the case; and if you want anything more than what EventArgs provides you have to create your own.



  • Kian01

    Heh. Amazing how people tend to over complicate things, eh
    class MyArgs : EventArgs
    { public Int32 x; public Int32 y;
    public MyArgs(Int32 x, Int32 y) { this.x = x; this.y = y; }
    public MyArgs() { } }
    // Proper code dictates that those should be properties but we're trying to be fast ;)

    delegate void MyDelegate(MyArgs e);

    Then you should either be able to pass EventArgs.Empty, null (not recommended just because then you always have to check if its null, or just pass a default MyArgs with no parameters (thats what the parameterless constructor was for)
    I used 's after the int's so that they can be null. This is great if your code wants to check to see if any parameters if for some reason you can't use EventArgs.empty or null.

    Hope this helps.


  • Frank ORourke

    That is why you will see alot of events use a class that is either or based off of EventArgs. This way you can pass EventArgs.Empty / or you can pass a structure that contains the necessary parameters.

  • how to point to a set of parameters using delegate