A better Dictionary/Hashtable. One that allows multiple entries per key.

Ever need the speed of a dictionary or hashtable, but also want to store more than one item with the same key? i.e. Maybe you have lists of items that need to be indexed against certain keys.

Maybe, Orders keyed by UserId. Well, enter the easy to use MultiDictionary. A class I put together to make it much easier to accomplish the goal. Speed and usability in one easy to use package.

[csharp]
public class MultiDictionary<K, V>
{
private Dictionary<K, List<V>> _dict = new Dictionary<K, List<V>>();

public void Add(K key, V val)
{
List<V> values;
if (!_dict.TryGetValue(key, out values))
{
values = new List<V>();
_dict.Add(key, values);
}
values.Add(val);
}

public bool TryGetValue(K key, out List<V> values)
{
return _dict.TryGetValue(key, out values);
}

public List<V> this[K key]
{
get
{
if( _dict.ContainsKey(key) )
return _dict[key];

return new List<V>();
}
set
{
_dict[key] = value;
}
}

public bool ContainsKey(K key)
{
return _dict.ContainsKey(key);
}

public IEnumerable<K> Keys
{
get { return _dict.Keys; }
}

public IEnumerable<List<V>> Values
{
get { return _dict.Values; }
}
}

public static class Extensions
{
public static MultiDictionary<TKey, TSource> ToMultiDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
MultiDictionary<TKey, TSource> md = new MultiDictionary<TKey, TSource>();
foreach (var item in source)
{
md.Add(keySelector(item), item);
}
return md;
}

}
[/csharp]
 

 

No more key not found exceptions either. If I can’t find the key, you get back an empty collection. You can still use the ContainsKey to determine if the item is absent. This just makes it so much easier to populate your entity objects with empty collections, instead of all the null checking.

Use just like a normal Dictionary, just expect a collection to be returned every time instead of one item.

Happy coding!
 

Windows Live Mesh Remote Connection no longer works

Well, I noticed my Live Mesh stopped working for remote connections. Did the software say why? Of course not.

I found this blurb from the jack a$$e$ at Microsoft who just silently killed the whole service. Could you they have sent us an email, notification, something?

"Windows Live Mesh was retired on February 13, 2013, and remote desktop and peer-to-peer syncing have stopped working. If you still have files in your Mesh online storage (called “Mesh synced storage” or “SkyDrive synced storage”) make sure you have copies of them on your computer. On August 13, 2013, all files in Mesh online storage will be permanently deleted."

Awesome,eh?

http://windows.microsoft.com/en-US/skydrive/mesh-users

Time to look for an alternative then.

How to handle action on enter key press in textbox in WPF

Did you want your OK button or your commit action to take place when you hit enter in a text field in a WPF dialog? You may have implemented a Find dialog, or maybe you have a one field text entry and you want the Enter key to commit the action.

Well, its actually really easy once you learn the simple trick to wiring up the commands.

I have a command called SaveProcessCommand on my viewmodel. I have it wired to my dialogs “OK” button. I also wanted the user to just hit Enter when they were done typing, since that is how my usability study determined users expect.

Enter the simple InputBinding. Pretty cool.

[csharp] <TextBox Margin="10,0,0,0" HorizontalAlignment="Stretch" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1">
<TextBox.InputBindings>
<KeyBinding Command="{Binding SaveProcessCommand}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
[/csharp]

I have the binding to the property update on each keystroke, so it is always up to date. The InputBinding then wires up the Enter key to a command. The command can do whatever you want, you are using MVVM right? Happy Coding!

How to reclassify your project into a WPF project in Visual Studio

How to change project type to wpf class library in Visual Studio 2012.

Are you looking to change your project from a normal C# class library to a Window Presentation Framework project?

Well, it is not that hard once you learn the trick. It took me a little while to dig up the info, so hopefully this saves you some time.

Open your csproj file in notepad. Look for the ProjectTypeGuids key. If not found, add it.

Here is what it should look like when your done.

<ProjectTypeGuids>{60DC8134-EBA5-43B8-BCC9-BB4BC16C2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Visual Studio 2012 and Visual Studio 2010 use these to identify the project type. The settings above make it a C# WPF app.

Example:

SNAGHTML77868191

 

Happy Coding!

Application file lock after debugging with Visual Studio 2012

I had a small WPF application that I was developing and every time I would stop it, make a quick small change then restart it would wait for a file lock then fail.

error message is : error LNK1168: cannot open [Yourfile].exe for writing

i.e. Visual Studio said it couldn’t open the file for writing. I just got used to waiting for about 30-60 seconds each iteration hoping the lock would go away. It was always the system that had the file handle.

Well, it seems that the issue was related to the fact I disabled the Application Experience service.

How random, right? Well, reenabling it has stopped the issue, for now.

image

Happy Coding!

Continue or Break using Parallel.ForEach

You might be converting your normal foreach loop over to use the Parallel.ForEach syntax to improve performance in a thread safe loop.

You also probably have some break and continue statements.

Well, it took me a few to figure out how to deal with these. Turns out, it is simple.

When you converted your loop into a compatible definition for the Parallel.Foreach logic, you ended up making the statement body a lambda, right. Well, that is an action that gets called by the Parallel function.

So, pretend it is a method. When you are done, you just return. So, replace your continues and breaks with return statements.

Easy, right? Requires the mental shift from built in operators, to library function calls.

Happy Coding!