Deserializing xml: Data at the root level is invalid. Line 1, position 1

A couple of days ago a colleague of mine encountered a problem where he had some objects that define a configuration needed to be stored in the database. He would serialize the data with the following code: public static class XmlSerializerHelper { public static string SerializeXml<TObject>(this TObject objectToSerialize) { using (var memoryStream = new MemoryStream()) […]

Check if your LinkedIn password appears in the leaked file.

Get the file from the internet. Change the line that points to the file. Run the app, type your password and be happy or sad. using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Security.Cryptography; using System.Text; namespace LinkedInFileSearcher { internal static class Program { private const string PathToLinkedInFile = @”c:\combo_not.txt”; private static void Main() { ConsoleKeyInfo […]

Get your facts right.

I just read this article: http://www.simple-talk.com/dotnet/.net-framework/10-reasons-why-visual-basic-is-better-than-c/ . And I’m pissed. Why? Allow me to elaborate First of all, because on the top right there is “a service from redgate”. You’d expect to get their facts right. And that’s the main issue with this article. There will always be fights among developers about which language is […]

Silverlight 4: Bug in TabControl.TabStripPlacement = Dock.Left and OnApplyTemplate

At work I ran into a bug with the Silverlight TabControl, more specifically when setting the TabStripPlacement to Dock.Left and hiding one of the TabItems in the parent’s OnApplyTemplate. The way to get to this bug is quite specific. For example, you can’t do it when you’re using a UserControl, since in that kind of […]

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 […]

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 […]