Previous month:
February 2010
Next month:
April 2010

March 2010

Better Together – C# and Visual Basic

C# 3 had new language extensions that are practical for every kind of application. No matter what application you’re creating, property initializers, collection initializers, Lambda expressions, LINQ… can increase productivity no matter if a Windows- or Web-application, a library, service… is created. That’s different with C# 4. The new language extensions are extremely useful for integrating with dynamic languages, .NET reflection, COM interop… Not every program created should use the dynamic keyword!

Looking at the extensions of Visual Basic 2010 it becomes very clear that a goal with C# and Visual Basic is that one language gets the features of the other. C# 3 introduced auto-properties, collection initializers, Lambda expressions… that were missing with Visual Basic. Wait, Visual Basic also had Lambda expressions. However, it was not possible to create multiline Lambdas. That’s possible now. Visual Basic 2010 also has auto-implemented properties and collection initializers.

Auto-Implemented Properties C# 3.0
public string FirstName { get; set; }
  VB 2010
Public Property FirstName As String
Collection Initializers C# 3.0
var names = new List<string>() { "James", "Jack", "Jochen" };
  VB 2010
Dim names = New List (of String) From { "James", "Jack", "Jochen"}
Implicit Line Continuation   This was never an issue with C# because ; is used. With Visual Basic 2010 now _ can be omitted.
Multiline Lambda Expressions C# 3.0
Func<int, int> expr = x =>
{
return x + 7;
}
  VB 2010
Dim expr = Function(x)
Return x + 7
End Function

Features that already have been in Visual Basic are now available in C# as well:

Optional Parameters C# 4.0
public void CreateRacer(string firstName, string lastName, int starts = 0, int wins = 0)
  VB
Public Sub CreateRacer(firstName As String, lastName As String, Optional Starts As Integer = 0, Optional Wins As Integer = 0)
Named Parameters C# 4.0
CreateRacer("Michael", "Schumacher", starts : 251, wins : 91);

  VB
CreateRacer("Michael", "Schumacher", starts := 251, wins := 91)
Dynamic C# 4.0
dynamic c = GetCalculator();
dynamic result = c.Add(3, 4);

  VB
Dim c = GetCalculator()
Dim result = c.Add(3, 4)

Dynamic allows calling into dynamic scripting languages such as Python, Ruby, JavaScript, it gives an easy way to use .NET reflection, and allows using the COM automation interfaces. Together with optional and named parameters this is really handsome! With Visual Basic 2010 the internal bindings have changed as well for interop with dynamic binders. Just the syntax of Visual Basic stayed the same with Option strict off.

C# 4 and Visual Basic 2010 also offer new features that haven’t been available with both of these languages. Among them are type equivalence support that is extremely useful with COM interop. With the /link compiler option (this is the same with both languages), COM type information can be embedded so that PIA assemblies are no longer required. Co- and contra-variance for interfaces and delegates is supported by setting the out and in keywords.

C# and Visual Basic are getting together – more and more. There’s not a lot difference in the features of these languages. The yield statement is still missing in Visual Basic – or did I miss such an enhancement in Visual Basic?

Depending on the preference of curly brackets or writing more text, the language of choice can be selected. For myself, with a background in C++ of course I prefer C#, but that’s really just a personal taste.

More information on C# 4 in my new book Professional C# 4 with .NET 4 and in my Take off to .NET 4 workshop.

Christian


Easing Animation with WPF and Silverlight

With WPF and Silverlight dependency properties can be animated using an animation to define a start and endpoint, e.g. DoubleAnimation to animate a property of type double.

If the animation shouldn’t be done in a linear fashion, a key-frame animation such as DoubleAnimationUsingKeyFrame can be used. With a key-frame animation interim points must be defined. Instead of defining key-frame animations, non-linear animations can also be defined with the DoubleAnimation type. WPF supports the AccelerationRatio and DecelerationRatio properties that define a percentage value in the timespan to be used with acceleration and deceleration. The sum of both AccelerationRatio and DecelerationRation may not exceed 1 and allow for a faster or slower start and end of the animation. There’s a restriction regarding Silverlight as these properties are not available there.

Silverlight 3 and WPF 4 have an even better story to influence the animation. With WPF 4 the animation classes support easing functions to change the animated values. The animation classes now defines the EasingFunction property of type IEasingFunction that allows assigning any object that implements this interface.

The following code snippets animates an ellipse using the easing function from the ElasticEase class. The ElasticEase class defines Springiness and Oscillations properties to change the elastic behavior.

<Ellipse x:Name="myEllipse" Fill="Red" Width="30" Height="30"
Canvas.Left="100" Canvas.Top="150">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="30" To="500" Duration="00:00:4"
Storyboard.TargetName="myEllipse"
Storyboard.TargetProperty="(Canvas.Left)">
<DoubleAnimation.EasingFunction>


<ElasticEase x:Name="myElasticEase" Oscillations="3"
Springiness="1" EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>

</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>

</Ellipse>


.NET offers several different easing functions to influence the animation. ElasticEase creates an animation that looks like a spring oscillating back and forth.

Classes that offer easing functionality implement the interface IEasingFunction. IEasingFunction defines the method Ease. Ease receives a time from the animation and returns the value that the animation should have that point in time. Usually there’s no need to implement the interface IEasingFunction directly but derive from the base class EasingFunctionBase instead. The method EaseInCore needs to be overridden deriving from this base class.

All the classes that derive from EasingFunctionBase contain the EasingMode property of type EasingMode. EasingMode allows setting if the easing function should influence in special the in, out, or both in and out of the animation by setting the enumeration values EaseIn, EaseOut, or EaseInOut.

Other classes available that offer easing functionality are BounceEase that animates a bouncing effect. The formulas of some of the easing functions are listed here.

Ease function Formula
BackEase f(t) = t³ – t * a * sin(t * π)
SineEase f(t) = 1 - [sin(1-t) * π/2]
QuadraticEase f(t) = t2
CubicEase f(t) = t3
QuarticEase f(t) = t4
QuinticEase f(t) = t5
PowerEase f(t) = t * power

You can compare the different easing functions with my Silverlight easing animation sample application. Here you can select the different easing functions and configure the easing function if applicable, and animate an ellipse from one line of the screen to another one.

SilverlightEasingAnimationjpg

More information on WPF and Silverlight in my Silverlight and WPF workshops and in my new book Professional C# 4 with .NET 4.

Christian