Linq

I’ve just found out how to combine .Sum() with a where clause, and all it took was a further step into the murky world of Linq.

I got my tip-off from a fellow BlogEngine.NET blog user Gary Kilminster – http://cunningplan.co.uk/post/Extension-Methods.aspx – although it doesn’t look like his blog has been updated for a while.

I had a list of custom objects, and wanted to get the sum of all their ‘Cost’ values as long as they had a specific property in common. The bit of Gary’s code that really made it click for me was:

        string londonNames = (from c in db.Customers
where c.City == "London"
select c.CompanyName).Sum();

In my case, it became:

        int result = (from o in objectList
where o.OptionGroupId == specificId
select o.Cost).Sum();

It’s just so simple and easy. Hooray!

.Net Nooks and Crannies

I really love Stack Overflow. It’s so cool to have a link to many, many great coding minds to help with the minutiae of my learning process, whether through asking a question myself or finding that someone else asked something similar months ago.

The last question I asked was about public properties that should be read-only (usually List<T> or somesuch thing), but allow the user to add an item still. The answer was to use a class called ‘ReadOnlyCollection<T>’. This is one of many, many classes that I’ve never heard of but would be incredibly useful if I had!

When I get to the end of the first run of Wargame Tools, I’ll definitely be refactoring it (especially since I plan to port the logic to an ASP.NET MVC 2.0 application and see how those work) and will be sure to blog about this class and how I find using it.

Wargame Tools – a learning program

My name is Pete, and I am a wargamer. I am also a programmer. As a programmer and huge geek, I look for ways to apply programming knowledge to my other hobbies. So it only makes sense that I would try to apply it to wargaming.

Over the years, I’ve seen, used and abandoned various ‘army list’ programs. My favourite for the longest time was Army Builder but their licence restrictions are just a little bit harsh for me – apparently, a recent ‘minor’ upgrade means that anyone without an active licence (mine ran out years ago) must buy a new one, and the datafiles are not compatible with previous versions. It sounds more like a major version change than a minor one to me, but that’s their call at the end of the day.

So I’ve decided to build my own, as daunting a task as it may be, and see if I can bring something I work on in my spare time to the web as a useful addition to the choices around in the world today. At the same time (and extending the possible release by an indeterminate amount) I decided to use this as an excuse to learn WPF and apply some more object-oriented concepts to my code.

One of the biggest stumbling blocks, as Army Builder has always worked with and Army Roster discovered this year, is that it’s not nice to just give away the intellectual property of games manufacturers. So there is a need to give people the tools they need to make the data files, with which to build the actual army lists. It’s also pretty difficult to take what amounts to a domain-specific language that your datafile program writes and your army list program understands, and present an easy, graphical way for your user to interact with it without getting them too deep into the technical sides of it. It needs to be accessible for non-programmers.

So far, I’ve been working away on it slowly, and through evenings and weekends when I can snatch a few minutes. I’ve found WPF and XAML to be very, very interesting to work with but there’s a bit of a learning curve to get over if (like me) you’re coming from a Windows Forms background. I will be putting up anything interesting that I’ve learned, or link to samples that have helped me, whenever I find something useful.

WPF for those who know WinForms

As a programmer, I am primarily self-taught. I’m at the stage now where some new technology articles I can read at the highest level and follow and understand, but others I am looking for the absolute most basic beginner’s guide around.

I recently began a project in WPF to try and learn what it could do – I find the best way to learn a new technology is to come up with an application for it and just go for it! The more complicated your application, the better, since it will force you to find more stumbling blocks, gaps in your knowledge, and interesting quirks. Of course, some things may just be a little too much to take on all at once (as I’ve discovered) and my first try was by no means going to be a releasable app! WPF was definitely in the category of ‘most basic beginner article please’ and although there are some beginner’s articles around, I just couldn’t get my head around what was going on.

I struggled for a while, then blamed it on the fact that WPF was really just very different to WinForms. It needn’t be – you can make a WPF app as if it were a Forms app – but to really use WPF to it’s potential, templates, styles, binding and the rest, there’s something very different about how to approach the problem. Accusing this of being my flaw (since a bad craftsman always blames his tools) I smashed in the relevant keywords to Bing and eventually crept upon jfo’s coding: Getting Started in WPF. The title of the document is “WPF for those who know WinForms” and it’s an excellent primer for… well… those who know WinForms to get their heads around WPF. There’s a lot of talk about the prototype and code names for what are now older versions of Visual Studio, but altogether it’s a good first resource. I just can’t seem to find any up-to-date blog from jfo, which is a shame given how helpful this document is!