7 Replies - 419 Views - Last Post: 05 December 2012 - 09:48 AM Rate Topic: -----

#1 Armitxes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 05:55 AM

Hello DiC Community!

I have a problem giving an extra value / variable to my event-function. As event, arrow.ManipulationCompleted gives the object and ManipulationCompletedEventArgs to my function "callOptions" but not the id!

Can someone help me? First time that i need to give an EventHandler an extra variable and I have no idea how to do this.

Thanks for your help!

Regards,
Armitxes

My code:
int exampleId = 12345;
arrow.ManipulationCompleted += callOptions;
// something as arrow.ManipulationCompleted += callOptions(arrow, arrow.ManipulationCompletedEventArgs, exampleId); would be awesome.

private void callOptions(object sender, ManipulationCompletedEventArgs e, int id)
        {
            ...
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Giving a extra Variable with EventHandler to a Method

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,658
  • Joined: 05-May 12

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 06:10 AM

Two questions:
1. Why do you need the ID? What is the ID associated with? If it's the sender, then set the ID on the sender and downcast the sender to the appropriate object so you can get access to it again.

2. How are you planning on wiring up the event handler? If you are planning on using the default event, then you are out of luck. If you are going to declare a new delegate and event that takes an ID as a 3rd parameter then that will work but will stick out like a sore thumb because it doesn't follow the normal pattern for events.

Alternately, you can derive from ManipulationCompletedEventArgs and add the ID in that derived class, but you would till have to declare the new class, a new delegate that uses the delegate, a new event, and still wire up the event.
Was This Post Helpful? 3
  • +
  • -

#3 Armitxes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 06:52 AM

The XAML object "arrow" (image) exists about 2000x on the XAML Page, problem is that every arrow is associated with another content & id.

ManipulationCompleted tells me that an arrow object was selected by the user but not which of the 2000 that's why i need the id of the associated content.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 07:58 AM

I think you are missing a basic understanding about something here.
You can't just add in an additional parameter to the event or event handler.

The signature for the event is (object, eventargs)
That's it. You can't make it (object, eventargs, somethingelse)

So you have no choice but to make it work with those two things.

You can either create your own eventargs class that takes more information with it, or send a different object in place of the sender.

The right way to do this as Skydiver suggests and inherit from the eventargs and give it additional information.

Read this article to get a better understanding of events
Quick and easy custom events
Was This Post Helpful? 2
  • +
  • -

#5 Armitxes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 08:06 AM

View PosttlhIn`toq, on 05 December 2012 - 07:58 AM, said:

I think you are missing a basic understanding about something here.
You can't just add in an additional parameter to the event or event handler.

The signature for the event is (object, eventargs)
That's it. You can't make it (object, eventargs, somethingelse)
Quick and easy custom events


Aw, okay thanks that's what I was unsure about. Will try to find another way then. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#6 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 08:53 AM

Hopefully this code illustrates how to use your own EventArgs.

using System;

namespace CustomEventArgsExample
{
    public class CustomisedEventArgs : EventArgs
    {
        public int CustomArgument { get; set; }

        public CustomisedEventArgs(int customArgument)
        {
            CustomArgument = customArgument;
        }
    }

    public class EventRaiser
    {
        private int mySecretProperty;

        public event EventHandler<CustomisedEventArgs> RaiseEvent;

        private void RaiseTheEvent()
        {
            if (RaiseEvent != null)
                RaiseEvent(this, new CustomisedEventArgs(mySecretProperty));
        }
    }

    class EventListener
    {
        private EventRaiser raiser;
        private int propertyFromRaiser;

        public EventListener()
        {
            raiser = new EventRaiser();
            raiser.RaiseEvent += new EventHandler<CustomisedEventArgs>(raiser_RaiseEvent);
        }

        void raiser_RaiseEvent(object sender, CustomisedEventArgs e)
        {
            propertyFromRaiser = e.CustomArgument;
        }
    }
}


Was This Post Helpful? 2
  • +
  • -

#7 Armitxes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 09:27 AM

Thank you!

Works now :)
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Giving a extra Variable with EventHandler to a Method

Posted 05 December 2012 - 09:48 AM

Also remember, the sender is typically the object that executed the event. This allows you to cast to it's type and use the information of that instance.

Assuming your Arrow class has a property for ID. If so, you could possibly do this...

private void callOptions(object sender, ManipulationCompletedEventArgs e)
{
   Arrow theArrow = sender as Arrow;
   int arrowID = theArrow.ID;
   // use 'arrowID'
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1