Passing values to event


I would just like to find out if it was possible to pass values on to an event without using methods and properties.

In my case I would like to pass values to the Canvas.Paint event and invalidate it to paint with additional values, If no other way I will consider using properties but I want to downscale on using properties as my app get’s quite hefty.

Thanks in advance.

  • Dian

EDIT: Maybe because it is quite some amount of values that i want to pass onwards, maybe create one property in array which contains all the values i want to display?

If your question is “Can I pass values to the Canvas Paint Event?” the answer is no, especially as you cannot call that event “directly” (only indirectly via REFRESH or INVALIDATE)

However if you make the “values” as properties of the window that owns that Canvas then

  • change the values as required
  • call myCANVAS.Invalidate

and define “hefty”… proper use of variables and properties is not necessarily a “bad thing”

Ah ok thank you, just a curious question, is it better passing values to a method or to create properties which the method retrieve values from?

That depends really on how you want to use the data in the variables. If you need to store the data and use it later I would use properties. If it’s okay to let go of the data at the end of the method use parameters to keep things minimal.

You may also want to look into subclassing the Canvas. You’d be able to implement your own methods to accept parameters, store them as properties, and refer to them in the Paint event. This would also encapsulate the properties to the Canvas, keeping the Window it’s on cleaner.

In addition to Tim’s reply, another consideration is if it is possible these events may have a chance to get queued and you need to operate on each of them individually later. With the properties method, the event handler would see the properties values set most recently. When part of an event’s parameters, each is available in turn.

So like most things in programming, there is not necessarily a right or wrong way, it really does depend on the scenario.

Another idea is to create a subclass and define your own paint event with what ever extra params. In the subclass you handle the paint event and raise a new one with extra params. Very close to what Tim suggested.

Oh I see, main reason I am asking is because there is more than one canvas and it gets updated every second with new values so I need the most efficient way of transferring variables which does not need to be kept for later use, I believe the subclass method would be considered the best solution in this case.

Thank you so much!