Wednesday, January 27, 2010

Flex Binding "Gotcha!"

Somewhere online, I saw a comment saying that binding in Flex is "evil magic" and should be avoided except in the simplest of cases. Though I could see where the author was coming from in theory, it wasn't until today that I encountered this in practice.

In short, setting a bindable variable to a field of a bindable class binds the variable to the value referenced by the field, not to the reference in the bindable class. Get it? The whole pass by value vs. pass by reference thing. Here's an example (ignore the use of the default package):

Class A:
package
{
[Bindable]
public class A
{
public var val : int;
public var b : B;

public function A()
{
val = 0;
b = new B(0);
}
}
}

Class B:
package
{
[Bindable]
public class B
{
public var val : int;
    public function B(v:int)
{
this.val = v;
}
}
}

MXML Test:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

<mx:Button click="onClick()" label="Click Me" />
<mx:Text text="A: {myA.val}" />
<mx:Text text="B: {myB.val}" />

<mx:Script>
<![CDATA[
[Bindable] var myA:A = new A();
[Bindable] var myB:B = myA.b;

function onClick() : void
{
myA.val += 1;
myA.b = new B(3);
}
]]>
</mx:Script>
</mx:Application>

At launch, the text elements both have the value 0. But, after one click of the button, what is the value of myB.val?

I don't think I'm alone on this, but maybe I am. In any case, I expected myB.val to be 3, because myB was assigned to myA.b and A is a [Bindable] class. When I changed the value of myA.b, I expected myB to reference new instance of B that myA.b now referenced.

Turns out it doesn't. After one click of the button, myB.val == 0. Why? Because myB is still bound to the original instance of B referenced by myA.b at the time myB was assigned, rather than whatever instance of B that myA.b might reference in the future.

Bummer. If only myB was a pointer...

24 comments:

happy wheels said...

Thank you very much for your post, it makes us have more and more discs in our life, So kind for you, I also hope you will make more and more excellent post and let’s more and more talk, thank you very much, dear.
happy wheels| friv|monster high| shooting games| tetris| 8 ball pool
cool math games| barbie games| friv4school| agario| fighting games

Rick Guerio said...

Thank you very much for your post, it makes us have more and more discs in our life, So kind for you, I also hope you will make more and more excellent post

box office collection of prem ratan dhan payo movie

thị hậu nguyễn said...

I want to share with you information about a reliable essays writing website
happy wheels
super mario bros
pacman
agario

Alice Taylor said...

Thank you for sharing valuable information. Nice post. I enjoyed reading this post.
learn to fly | Learn To Fly 2 | Learn To Fly 3 | Pacman | Pacman game | Pacman games | Mahjong | Mahjong game | Mahjong online| Happy Wheels | happywheels| happy wheel| Monkey Go Happy | Monkey Go Happy 2| Monkey Go Happy 3 | cat mario 4| cat mario | cat mario 3

lân Mai said...

I just want to say : why you do not click to play
puzzle games | mario games | brain games | tetris | happy wheels | agario | super mario

Lydia Loftis said...

Flex binding and the bindable variable - I have plenty of doubts regarding these two important components of programming. I found this site when searching for
buy dissertation online page. I need a technology related article.

Rafien Mafinha said...

What could be more wonderful.
Game of Thrones | Glory | Armor

Kelly Anni said...

I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!
Science Kombat | Science Kombat Game | slither io | wings io | wingsio | Tank trouble | Tank trouble 2 | Tank trouble 3 | Tank trouble 4

Lopez Alexander said...

This content is written very well. Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you.
- usps tracking
- iphone 7 release date
- excel

Five Nights At Freddy's said...

Thanks for giving me the useful information. I think I need it!
Happy Wheels , FNAF World , Five Nights At Freddy's

Alice Denny said...

Welcomes to google terminal keep sharing such ideas in the future as well.
google snake this was actually EARN TO DIE
what i was looking for,and i am glad to came here! AGE OF WAR 2
Hi! I’ve been reading your blog for a while HAPPY WHEELS
I want you to thank for your time of this wonderful read!!! Slitherio
Amazing insight you have on this, Slither io This article always blew me BIG FARM

Jasmine Thompson said...

Chaos Faction 2 with 29 weapons, 18 characters, 15 levels, 14 months in the making... the best games you will like its, have fun! Super mechs is a turn-based online strategy multiplayer game where you build your very own mech and use it to dominate your opponents, be they A.I. or other players like yourself! Mahjong free games online is a puzzle game based on the same tiles. The goal is to match open pairs of identical tiles and remove them from the board, exposing the tiles under them for play

Anh luu said...

thanks for post..it was very useful for me. click to play happy wheels 2 demo

Anh luu said...

thanks for post..it was very useful for me. click to play happy wheels 2 demo

Alice Denny said...

The blog or and best that is extremely useful to keep I can share the ideas. Age Of War 2
Big Farm | Slitherio | Tank Trouble
Of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
Happy Wheels | Goodgeme Empire | Slither.io


Friv3 said...

I am impressed by the details that you have on this article. Mark this website, will come back for more articles. Good job. Thanks! Friv2 Unblocked
Moto X3M3 hacked

Lang Nguyen said...

Troll Face Quest Video Games. Play. Troll Face Quest Video Games · Y10 City Heroes. the best free online Twizl games, juegos Twizl, jogos Twizlfrom the top game developers all over the world. Zoxy is the best entertainment site with free games.

Lang Nguyen said...

Best of friv 4 Games game series are waiting for you! Kizi 100 Games is a great place to play free online juegos kizi game, sport, girl, including puzzle, boy and more juegos de kizi 100 game! Enjoy and play Kizi!

Xbox One said...

This life is short, do not spend even a minute ... to people, things that make you sad.
Friv 2 Games | Unblocked Gammes 77 | Yepi 2 | Unblocked Games | Unblocked at School | Unblocked Games66 | Unblcoked Minecraft | Unblocked Games | Pbs | Unblocked

Bảo Gia said...

Thank you very much for your post, it makes us have more and more discs in our life, So kind for you, I also hope you will make more and more excellent post and let’s more and more talk, thank you very much, dear.
friv 100 | Friv 10 | zoxy | agar.io

instagramsignin said...

www.instalogin.com
www.logininstagram.com
www.instagramlogincom.com

instagramsignin said...

instagram-signin.com
www.merrychristmasgreetingss.com
love is good quotes

Alexander Sara said...

Thanks for sharing this quality information with us.
super smash flash 2 unblocked | strike force kitty 2 | super smash flash | strikeforce kitty 2 | super smash flash 2

aaa kitty20101122 said...

adidas nmd r1
http://www.kobeshoes.uk
nike huarache
yeezy shoes
michael kors purses
lebron james shoes
kyrie shoes
jordan shoes
air yeezy
longchamp bags