Dictionary<TKey, TValue> finding key: try/catch vs TryGetValue

Today I saw some code where the developer accessed a dictionary like this: void Foo(int keyToFindInDictionary) {     Dictionary<int, string> dictionary = new Dictionary<int, string>();       try     {         DoSomethingWithTheResult(dictionary[keyToFindInDictionary]);     }     catch (KeyNotFoundException)     {         // fallback     } }   private void DoSomethingWithTheResult(string s) {     // do something } As you can see he catches the exception to […]

Enum dependency property caveat

Hi all, A while ago I was creating a custom control in Silverlight with an enum Dependency property. It looked like this: public partial class SomeCustomControl { public static readonly DependencyProperty OnOrOffDependencyProperty = DependencyProperty.Register(“OnOrOff”, typeof (OnOff), typeof (SomeCustomControl), new PropertyMetadata(OnPropertyChangedCallback)); public SomeCustomControl() { this.InitializeComponent(); } public OnOff OnOrOff { get { return (OnOff) this.GetValue(OnOrOffDependencyProperty); } […]

Stopping Casini before (re)building website in Visual Studio

Hi ya’ll, I sometimes use Casini to debug local web applications. Casini is easy to configure, no need to create a virtual directory, it’s gone after you close Visual Studio so it’s easy. The problem is that sometimes when changing some code, Casini’s caches aren’t cleared. So you get a stale version of your webpage […]

Visual Studio 2010 PowerTools

Hi All, Since I’ve installed Visual Studio 2010 PowerTools I’ve had the following problem: Entering shift+enter didn’t break the line anymore. This is because of the autocomplete braces function of the PowerTools. You can disable this function by clicking Options in the Tools Menu: And then click ‘Productivity Power Tools’ on the left: In that […]

Windows Phone 7 TextBox, binding and ApplicationButton

Consider a Windows Phone 7 application with a textbox and an ApplicationButton. UI: <Canvas x:Name="ContentGrid" Grid.Row="1" Height="545" Width="480">     <TextBlock Text="Enter text here" Canvas.Left="6" Canvas.Top="187" />     <TextBox x:Name="myTextBox" Text="{Binding Test, Mode=TwoWay}" Height="69" Width="480" Canvas.Left="0" Canvas.Top="209" /> </Canvas> Backend: namespace WindowsPhoneApplication1 {     using System;     using System.ComponentModel;     using System.Windows.Controls;     using Microsoft.Phone.Controls;       public partial class MainPage : PhoneApplicationPage, […]

A more general INotifyPropertyChangedHandler extension method for strongly typed reflection

static class ExtensionMethods {     public static void Raise<TValue>(this PropertyChangedEventHandler handler, Expression<Func<TValue>> propertySelector)     {         var memberExpression = propertySelector.Body as MemberExpression;           if (handler == null || memberExpression == null)         {             return;         }           var sender = ((ConstantExpression)memberExpression.Expression).Value;         handler(sender, new PropertyChangedEventArgs(memberExpression.Member.Name));     } } Use this as an extension method like this on a class that implements INotifyPropertyChanged […]

MvvmLight: strongly typed INotifyPropertyChanged

Since MvvmLight exposes it’s own ViewModelBase you cannot use this little script to raise the INotifyPropertyChangedEventHandler, because .NET doesn’t allow you to raise events in derived classes. This is because the compiler generates a private delegate (it’s in VB.NET but that doesn’t matter). And since the ViewModelBase only exposes a protected RaiseProperty(string propertyName) I cannot […]