.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Merry Christmas and Happy New Year

I hope everyone had a wonderful Christmas.

I was designated cook at my family's gather and as tradition, on Christmas eve, we have a seafood feast. First on the menu was Mussels Ala Marinara served with crustinies. This was followed with garlic shrimp, calamari and the main dish salmon with butter amaretto sauce.

On Christmas day I visited my cousin's home and spent the day with my relatives. It was a joy to see everyone especially the children. Santa was good to them and you could see it on their faces.

But what does this post have to do with .NET? Well nothing, really, but if you need a dose of .NET then visit my  Deep Zoom Holiday Site  and see if you can find some of my family members!

Enjoy the rest of the holiday's.

Guess the movie

You see George, you've really had a wonderful life. Don't you see what a mistake it would be to just throw it away?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Saturday, December 27, 2008 8:14 AM
Permalink | Comments (18) | Post RSSRSS comment feed

Yours truly on UGTV

Back in May 2008, I had the privilege of representing TVBUG at the Canadian User Group Summit held at DevTeach, Toronto. It was great meeting with .NET user group representatives and discussing relevant issues.

The User Group TV (UGTV) was also in attendance and interviewed us for an future episode of UGTV. The latest episode of the show features interviews with the user group representatives. Check me out!

Guess the movie

Remember, George: no man is a failure who has friends.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: User Group
Posted by CynotWhyNot on Friday, December 12, 2008 11:20 AM
Permalink | Comments (7) | Post RSSRSS comment feed

Blindly calling RaiseEvent in VB.NET

I have seen far too many examples where VB.NET code does not check to see if there are any listeners prior to raising the event. The C# examples seem to always check. For example, take a look at the MSDN code example for INotifyPropertyChanged. The two snippets below are from this site.

Raising an event in C# taken from MSDN 

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info) {

    if (PropertyChanged != null) {

        PropertyChanged(this, new PropertyChangedEventArgs(info));

    }

}

Raising an event in VB.NET taken from MSDN 

Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged
 
Private Sub NotifyPropertyChanged(ByVal info As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub

The above two snippets are fairly typical. The C# example checks to see if there are any listeners and only if there are does it raise the event. Contrast that to the VB.NET code where the RaiseEvent is called regardless of whether there are any listeners to the event.  Both code examples work fine. It would seem in VB.NET the RaiseEvent call will not throw a null exception when there are no listeners.

How do we check for listeners in the VB.NET world. The obvious would be to add If PropertyChanged IsNot Nothing Then but you'll quickly find referencing PropertyChanged in this manner will not compile. The proper way is shown in the listing below.

Checking for subscribers in VB.NET

 

    6     Public Event PropertyChanged( _

    7         ByVal sender As Object, _

    8         ByVal e As System.ComponentModel.PropertyChangedEventArgs) _

    9         Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

   10 

   11     Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)

   12         If PropertyChangedEvent IsNot Nothing Then

   13             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))

   14         End If

   15     End Sub

The trick is to add the Event suffix to the event name, in our case PropertyChangedEvent, and check to see whether it has a value. So why don't we not see this code pattern more often. Firstly, it doesn't seem to be well documented and secondly this reference variable is not reported by intellisense.

The question is it good practise to check whether there are any listeners? In C# it is a given, otherwise, a null reference exception will be throw. In VB.NET no exception will be thrown and the call to RaiseEvents will not throw an exception when there are no listeners. But which is faster? I wrote a simple application to check the amount of time it takes to blinding call RaiseEvent versus the check for the existence of the listener. Checking the listeners prior to calling RaiseEvent is about 1.75 faster than just calling RaiseEvent blindly. It should be noted that even though the check is 1.75 times faster, the actual amount of time to call RaiseEvent is extremely small. A call to RaiseEvent with no listeners takes approximately 0.000000008 seconds.

In the end, checking for listeners, in VB.NET, will in all likelihood not impact the performance of the code.

Guess the movie

Why am I such a misfit? I am not just a nitwit. You can't fire me, I quit. Seems I don't fit in.

Currently rated 4.0 by 6 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB.NET
Posted by CynotWhyNot on Tuesday, December 09, 2008 6:38 PM
Permalink | Comments (73) | Post RSSRSS comment feed

Using an Intermediary to implement binding between Silverlight controls

WPF has built-in support for binding UI elements. For example, when the user changes the position of the slider the width of the button changes, all done through binding in XAML. Unfortunately, Silverlight does not have this built-in support. So what can be done?

Using an intermediary class and generics it is fairly simple to roll your own. Let's take a look at how it works.

Using VS2008 create a Silverlight application. I called the project BindingBetweenUIControls, rename the TestPage's to default and am using VB.NET. Next, in the Silverlight project, add a class called IntermediaryUIBinding.vb. Figure 1 shows the solution explorer.

Figure 1: Solution Explorer

solution  

Edit the class InterediaryUIBinding.vb. Listing 1 shows the complete listing of this class.

Listing 1: The class IntermediaryUIBinding used to bind UI controls.

    1 Imports System.ComponentModel

    2 

    3 'Intermediary class used to binding to UI properties of type double.

    4 Public Class IntermediaryUIBinding(Of T)

    5     Implements INotifyPropertyChanged

    6 

    7     Public Event PropertyChanged( _

    8         ByVal sender As Object, _

    9         ByVal e As System.ComponentModel.PropertyChangedEventArgs) _

   10         Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

   11 

   12     Private _IntermediaryValue As T

   13     Public Property IntermediaryValue() As T

   14         Get

   15             Return _IntermediaryValue

   16         End Get

   17         Set(ByVal value As T)

   18             _IntermediaryValue = value

   19             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IntermediaryValue"))

   20         End Set

   21     End Property

   22 

   23     Public Sub New(ByVal value As T)

   24         IntermediaryValue = value

   25     End Sub

   26 

   27 End Class

This generic class implements INotifyPropertyChanged which is a requirement if you want to provide two way data binding. Please refer to the previous post Silverlight Data Binding, using INotifyPropertyChanged and ObservableCollection. The generic implementation permits this class to be used for UI properties of various types. In this example we will be binding a double value. The actual class implementation is extremely simple containing a single property that raises the PropertyChanged event whenever the value is modified. This event is used by the binding infrastructure to notify the UI of data changes.

The UI for this Silverlight control is shown in listing 2.

Listing 2: Page.xaml with the UI data binding markup.

    1 <UserControl x:Class="BindingBetweenUIControls.Page"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    >

    5     <Grid x:Name="LayoutRoot" Background="Bisque" Width="400" Height="300">

    6         <Grid.RowDefinitions>

    7             <RowDefinition Height="150"/>

    8             <RowDefinition/>

    9             <RowDefinition/>

   10         </Grid.RowDefinitions>

   11         <TextBlock FontSize="18" TextWrapping="Wrap" Margin="10">

   12             <Run>Using an intermediary object you can bind properties of controls.</Run>

   13             <Run>Change the slider position and watch the button width change.</Run>

   14         </TextBlock>

   15         <Slider

   16            x:Name="bindingSlider"

   17            Value="{Binding IntermediaryValue, Mode=TwoWay}"

   18            Width="300" Minimum="100" Maximum="300"

   19            Grid.Row="1" />

   20         <Button

   21            x:Name="bindingButton"

   22            Width="{Binding IntermediaryValue}" Height="50"

   23            Content="Binding UI" Grid.Row="2" />

   24     </Grid>

   25 </UserControl>

The markup contains two controls; a slider and a button. The slider, line 17,  has it's Value attribute bound to the IntermediaryValue property and uses two way data binding. Similarly , the button control, line 22, has it's Width attribute bound to the same IntermediaryValue property but does not use two way data binding. The two way binding for the slider is necessary since we want the underlying data stored in the property  IntermediaryValue to change whenever the slider position is changed by the user.

Finally, all that needs to be done is to set the DataContext of a parent control. This is easily done in the code behind of Page.xaml and is shown in listing 3.

Listing 3: Page.xaml,vb Codebehind

    1 Partial Public Class Page

    2     Inherits UserControl

    3 

    4     Public Sub New()

    5         InitializeComponent()

    6     End Sub

    7 

    8     Private Sub Page_Loaded( _

    9         ByVal sender As Object, _

   10         ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

   11         LayoutRoot.DataContext = New IntermediaryUIBinding(Of Double)(bindingSlider.Value)

   12     End Sub

   13 

   14 End Class

Fairly simple, line 11 sets the DataContext of the grid control to an instance of the Intermediary class specifying a generic type of Double. The constructor parameter specifies the initial value of the slider. Now when the users changes the position of the slider the binding infrastructure will update the underlying data store, i.e., the property  IntermediaryValue and since this property is bound to the button's width it will change accordingly.

That's it binding between Silverlight UI controls.

This post was inspired by a presentation by Dan Borkowski at the Toronto Silverlight User Group.

If you would like to see this code in action or would like to download the code then visit my Silverlight Demo Gallery.

Guess the movie

Where do you think you're going? Nobody's leaving. Nobody's walking out on this fun, old-fashioned family Christmas. No, no. We're all in this together. This is a full-blown, four-alarm holiday emergency here. We're gonna press on, and we're gonna have the hap, hap, happiest Christmas since Bing Crosby tap-danced with Danny fucking Kaye. And when Santa squeezes his fat white ass down that chimney tonight, he's gonna find the jolliest bunch of assholes this side of the nuthouse.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Thursday, December 04, 2008 3:30 PM
Permalink | Comments (68) | Post RSSRSS comment feed