locked
Passing Paramters in EventHandler RRS feed

  • Question

  • Hi

    How can i pass Parameters from a EventHandler to my function? I need the variable TEST in my second funcion.

    void Suche_Messstellen(object sender, EventArgs e)
    
    {
    
    string test = item.Messstelle;
    
    ws.Load(ws.GetWasserstandsQuery()).Completed += new EventHandler(LineChart_Erzeugen2);
    
    }
    
    
    void LineChart_Erzeugen2(object sender, EventArgs e)
    
    {
    }
    
     
    Wednesday, July 7, 2010 7:31 AM

Answers

  • I don't believe you can do that. An event handler sends a reference to the object that raised the event and the event args (which you may be able to define and instantiate - but I don't know how) that are associated with that handler. When I've wanted to pass data the way you want to, I've always used a class level variable.

    Wednesday, July 7, 2010 7:36 AM
  • You need to have the completed event as thats when your data gets loaded untill then its null and you wont be able to use it.

    HTH. Mark as answered if that helps :-)

    Regards,

    Vidds

     

    Wednesday, July 7, 2010 8:13 AM

All replies

  • Hi,

    One solution would be,

    You can keep the variable global to the page and access in your new function.

    HTH. Mark as answered if that helps :-)

    Regards,

    Vidds

     

    Wednesday, July 7, 2010 7:34 AM
  • I don't believe you can do that. An event handler sends a reference to the object that raised the event and the event args (which you may be able to define and instantiate - but I don't know how) that are associated with that handler. When I've wanted to pass data the way you want to, I've always used a class level variable.

    Wednesday, July 7, 2010 7:36 AM
  • You need to write your own delegate which expects your variable / object as parameter

     

    namespace SilverlightApplication1
    {
        public delegate void MyHandler(string name);
    
        public partial class MainPage : UserControl
        {
            public event MyHandler MyEvent;
    
            public MainPage()
            {
                InitializeComponent();
    
                Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                this.MyEvent += new MyHandler(MainPage_MyEvent);
    
                MyEvent("Donald Duck");
            }
    
            void MainPage_MyEvent(string name)
            {
                //Do something with name
            }
        }
    }
     
     
    Wednesday, July 7, 2010 7:39 AM
  • Hi,

    You can't do that. You have to make it a global value and then you can access it inside your EventHandler.

    Wednesday, July 7, 2010 7:39 AM
  • Hmmm ok,..maybe there is a different way to solve my whole Problem.

    ws.Load(ws.GetWasserstandsQuery()).Completed

    After the Loading of my DomainContext is Completed,...is it the only way to create a new EventHandler?

    I dont know exactly how to explain this, but i want to load my Data and then use it without going to another EventHandler,...

    Wednesday, July 7, 2010 8:05 AM
  • You need to have the completed event as thats when your data gets loaded untill then its null and you wont be able to use it.

    HTH. Mark as answered if that helps :-)

    Regards,

    Vidds

     

    Wednesday, July 7, 2010 8:13 AM