From: Lyle Johnson Date: 2003-01-15T04:35:31+09:00 Subject: Re: [FxRuby] Question on FXDataTarget Joey Gibson wrote: > I'm using FOX via FxRuby and I'm trying to get FXDataTarget going. If I > wrap a local variable in an FXDataTarget and then put a FXTextField around > that, everything seems to work fine. IOW, this works > > @fooDt = FXDataTarget.new(0) > @foo_input = FXTextField.new(self, 5, @fooDt, FXDataTarget::ID_VALUE, ...) > > and then in a button's method > > @fooDt.value = 99 > > the result is that the text field get's updated like it should. But what if > I have an object whose members I want to connect to text fields? IOW, I > have this class > > class Foo > attr_accessor :bar > end > > @foo = Foo.new > @fooDt = FXDataTarget.new(@foo.bar) > @foo_input = FXTextField.new(self, 5, @fooDt, FXDataTarget::ID_VALUE, ...) > > and then in a button's method > > @foo.bar = 99 > > the textfield does not get updated, even though the attribute of the object > has been updated. > > Am I doing something wrong here? The only thing you're doing wrong is attributing more power to the FXDataTarget than it actually has ;) An FXDataTarget is a special kind of object that acts as a wrapper around some other object (its value). As you've already discovered, you can get or set the value of this wrapped-up object via the FXDataTarget#value() and FXDataTarget#value=() accessor methods, e.g. fooDt = FXDataTarget.new(0) fooDt.value = 99 theValue = fooDt.value When a widget like FXTextField interacts with a data target, it (indirectly) uses these accessors to get and set the value of the data target. But when you initialize a data target with some value, e.g. fooDt = FXDataTarget.new(@foo.bar) it just sets the value of its wrapped-up object to the current value of @foo.bar. In other words, both @foo.bar and fooDt.value are now references to the same object. But -- and this is the important part -- there's no way for the data target to change which object @foo.bar points to.