Skip to main content

Binding values after object creation

2 replies [Last post]
chrono_legionair
Offline
Joined: 2007-12-21

Hi,

I am wonder how to set a bind for an attribute when the object itself has already been created. For example,

<br />
class ModelX {</p>
<p>    attribute a: Integer;</p>
<p>    attribute b: Integer:<br />
}</p>
<p>...</p>
<p>class ModelY {<br />
    var x: ModelX = ModelX {<br />
        a: bind 10;<br />
    }</p>
<p>    // trying to do something like below but does not work<br />
    x.b = bind 20;<br />
}<br />

Any help is appreciated.

Thanks

Message was edited by: chrono_legionair

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tball
Offline
Joined: 2003-06-10

It looks like bind isn't being used properly, as here you only want to set a property value:

var x = ModelX {
a: 10;
}
x.b = 20;

The bind operator is used to keep one variable in sync with another. Your "a: bind 10" expression compiles but doesn't do any useful binding, since the "10" expression can't change (it's a constant). If you have experience working with Swing, a common time to use bind is when in Swing you would create a PropertyChangeListener to keep two variables in sync.

Tom

chrono_legionair
Offline
Joined: 2007-12-21

Thanks for your reply Tom, I used 10 and 20 just as some random value (bad idea), I should have written

var x = ModelX {
a: bind someValue;
}
x.b = someOtherValue;

I am trying to achieve something like I bind something to a value and want to change the binding later on. So, variable X used to bind to expression A, and I want to change it so variable X now bind to expression B. Is it possible?

The reason for this is Class X has an attribute which I need to bind to Class Y. However, no instance of Class Y is available when I am creating Class X. So I create an instance of Class Y (after X is already created) and then bind it to the attribute from X.

Message was edited by: chrono_legionair