Binding Operators for C# 5

by KodefuGuru 3. December 2009 11:56

I read this article from Martin Calsyn on the new Microsoft Visualization Language called Vedea, and I must admit that I was struck by how awesome binding operators actually are. This is a feature that must make its way into C# 5! Here’s an example of a binding from his article.

textbox.Text := slider.Value;

This code looks suspiciously like Delphi. In Pascal (Delphi uses Object Pascal), := is an assignment operator. In Videa, := is a left binding assignment. What happens is that anytime the slider’s Value property is changed, the textbox.Text property updates to reflect that change.

textbox.Text :=: slider.Value;

The :=: operator is a two-way binding operator. Now, if the textbox.Text property is modified, slider.Value is changed as well.

Martin then shows just how powerful these bindings are.

myData = DataSet(“mydata.csv”); 
currentYear := slider.Value + 1900; 
bubbles := from row in myData  
  where row.Year :== currentYear  
  select new Circle()  
    {  
      X = row.Latitude,  
      Y = row.Longitude,  
      Radius = row.Population * scalingFactor,  
      Fill = BlackBodyPalette(1., 1., row.DeltaCarbon)  
    }; 
Scene[“USMap”].Add(bubbles);

This code loads data from a CSV file (similar to Jim Wooley’s LINQ to CSV) and assigns it to the myData variable. It then binds the slider.Value + 1900 to the currentYear variable; this will be used in our LINQ binding. Then a query on the data is bound to the bubbles variable which is used to show bubbles on a map. Inside of the LINQ statement, there is a boolean binding operator, :==, which assigns currentYear to the row.Year. This combines to have the powerful effect of changing the bubbles on the map to reflect the data in the CSV whenever you move the slider.

As you can see, this code is extremely powerful. It was made possible by the introduction of the DLR into the .NET framework. Kudos to the Microsoft Research team in Cambridge, UK for making such an awesome language. Now please make sure it makes its way into C# 5!

Tags: , , ,

Kodefu

Comments

12/3/2009 11:57:27 AM #

trackback

Trackback from DotNetKicks.com

Binding Operators for C# 5

DotNetKicks.com

12/3/2009 12:32:41 PM #

Justin James

Now *that* is a very cool feature! It basically encapsulates the whole event handler/screen update code that gets very, very tedious to write. Smile

J.Ja

Justin James United States

12/3/2009 12:33:29 PM #

Justin James

That being said, I am curious to know how it works when the value is updated on a thread other than the UI thread, one of those situations where you would normally use Invoke() to have something occur.

J.Ja

Justin James United States

12/3/2009 1:24:41 PM #

Chris

It used dynamic binding, so it should work fine. However, Nivea bits won't be released until early next year so I can't analyze it yet. I do think this is something that will make its way into the framework much like the features in Cw became LINQ.

Chris United States

12/4/2009 9:34:25 AM #

Lucio Biondi

Uhm... seems to me like a demo I wrote six months ago.

http://www.olijo.org/gvision/?p=17

Lucio Biondi Italy

12/4/2009 11:10:26 AM #

chris

Thanks for the link, Lucio! It appears this concept isn't exactly new (hooking it up with Linq is powerful though). I will take a look at Givvy.

chris United States

12/5/2009 12:00:19 PM #

trackback

Trackback from Sanjeev Agarwal

Daily tech links for .net and related technologies - December 5-6, 2009

Sanjeev Agarwal

12/6/2009 12:17:03 PM #

ramooon

Why does the operator looks like this ?? It is SO UGLY. I would love to have this instead:

textbox.Text  --> slider.Value;
textbox.Text <--> slider.Value;

This is much more readable !!

ramooon France

12/6/2009 12:50:58 PM #

Chris

Be careful with the ??, ramooon. When I first read your comment, I thought you were asking about the null coalescing operator =).

As a C# developer, I prefer := and :=: over --> and <-->. The = operator already means assignment to me. adding a colon to it conveys that it's sticky. I can easily switch back and forth between assignment and binding assignments with that syntax.

Chris United States

12/7/2009 11:47:02 PM #

Jaans

Chris, I totally agree. To me this is a vary "natural" way of expressing binding relationships, especially if you consider todays binding mechanism of WPF/Silverlight together with DependencyProperties.

Why not create a Microsoft Connect issue for a feature request?

Ps: --> and <--> feels to much like pointer dereferencing in C++ not event to mention the fact that it's an additional keystroke ;-p

Jaans Australia

12/9/2009 11:32:11 AM #

Andy Kutruff

I love reactive programming.

Check out CLINQ 2 and the class ReactiveObject.  It does exactly this type of paradigm for you, is super fast, and uses an optimized weak event system.  I would have had to do a lot less work if C# had property change notification build into the language.

My blog post about it: kutruff.wordpress.com/.../

Andy Kutruff United States

12/11/2009 4:57:14 PM #

Marc Brooks

Each day, we get closer to the binding of the C++ class library of the product "Utah" I played with in the early 90s.  I'm not sure that's a good thing.

Marc Brooks United States

12/23/2009 8:03:56 AM #

rahul



First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the

information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for community.

There is a good C# resource site, Have alook

http://CSharpTalk.com

Thanks again
Rahul

rahul India

3/5/2010 4:22:09 PM #

Kenneth

I learn something new everyday or get to read about other people's experience which is why I love reading blogs.

Kenneth Israel

3/9/2010 8:52:39 AM #

Why

Very useful stuff this, as a programmer by trade, free lance that is, it is nice to have blogs like these to go to for useful information.

Why Turkey

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010