English 中文(简体)
Is it possible to create eventlisteners for string, int, bool at all?
原标题:

Is it possible at all to create eventlisteners (i.e. when the value changes) for a variable of type string, int, bool, etc.?

I haven t seen this in any programming language so far, except for some Collections (like ArrayCollection in Flex), which use events to detect changes in the collection.

If not possible at all, why not? What s the reason for this? Are there any best practices to achieve the same sort of functionality? And what about extending functionality with databinding?

最佳回答

I guess this sort of functionality is not added in any framework/runtime since it would create a big overhead (think on how many times you modify a variable holding a primitive type within the average application) while being not used under normal circunstances.

Anyway, in .NET at least (and I guess that in other OO environments as well), you can define properties, which are accessed as normal variables but can have associated code that reacts when its value is read or modified.

问题回答

I don t think there is anything by default, however, you can create a custom event and raise it on the set of the method. Something like...

C# example

public delegate void MyValueChangedEventHandler(bool oldValue, bool newValue);
public event MyValueChangedEventHandler MyValueChanged;
private bool myValue;
...

public bool MyValue
{
    get { return myValue; }
    set
    {
        if (myValue != value)
        {
            var old = myValue;
            myValue = value;
            MyValueChanged(old, myValue); 
        }
    }
}

It is possible if you wrap your variables in getters and setters and fire the event when the setter is called.

How about using setter methods and having them register events when changing the value of the variable?

In general, no. The reason is that primitive types are simply bits and bytes stored in some memory location: changing the data in that memory location does just that, and nothing else. Firing events would require calling some methods/functions. So the functionality can be achieved by wrapping the primitive types in some kind of wrapper objects - but of course, they re not 100 % interchangeable: for instance Java s primitive wrapper types (Integer etc.) are marked final, so it s not possible to extend them with event-firing versions to take advantage of auto(un)boxing.

Another approach is to poll the variable frequently and fire appropriate events if it has changed. This is a "dirty" approach with obvious disadvantages (performance overhead, not immediate reaction), but could regardless be useful in some situations. If you do this from another thread in Java, be sure to mark the variable volatile.

It is possible to create listeners, as some of ther others have mentioned, by making a class that fires an event whenever a property changes. This is obviously a lot less efficient than just assigning a value, but there are cases where it could be useful.

Some languages (VB6 and some others) have the ability in debug mode to stop execution when the value of a variable changes. I haven t seen this in .net, but it s liable to be in there somewhere. :-)

It seems to me that using an event to signal a simple variable change could be accomplished with if statements at each assignment, unless the value that variable is being changed externally, in which case you could use a class to handle it.





相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签