February 2011 Entries

Microsoft provide an article of the same name (previously published as Q319401) and it shows a nice class 'ListViewColumnSorter ' for sorting a standard ListView when the user clicks the column header.

This is very useful for String values, however for Numeric or DateTime data it gives odd results. E.g. 100 would come before 99 in an ascending sort as the string compare sees 1 < 9. So my challenge was to allow other types to be sorted. This turned out to be fairly simple as I just needed to create an inner class in ListViewColumnSorter which extends the .Net CaseInsensitiveComparer class, and then use this as the ObjectCompare member's type.

Note: Ideally we would be able to use IComparer as the member's type, but the Compare method is not virtual in CaseInsensitiveComparer , so we have to create an exact type:

public class ListViewColumnSorter : IComparer
{
    private CaseInsensitiveComparer ObjectCompare;
    private MyComparer ObjectCompare;

    ... rest of Microsofts class implementation...
}

Here is my private inner comparer class, note the 'new int Compare' as Compare is not virtual, and also note we pass the values to the base compare as the correct type (e.g. Decimal, DateTime) so they compare correctly:

private class MyComparer : CaseInsensitiveComparer
{
    public new int Compare(object x, object y)
    {
        try
        {
            string s1 = x.ToString();
            string s2 = y.ToString();
 
            // check for a numeric column
            decimal n1, n2 = 0;
            if (Decimal.TryParse(s1, out n1) && Decimal.TryParse(s2, out n2))
                return base.Compare(n1, n2);
            else
            {
                // check for a date column
                DateTime d1, d2;
                if (DateTime.TryParse(s1, out d1) && DateTime.TryParse(s2, out d2))
                    return base.Compare(d1, d2);
            }
        }
        catch (ArgumentException) { }
 
        // just use base string compare
        return base.Compare(x, y);
    }
}

You could extend this for other types, even custom classes as long as they support ICompare.

Microsoft also have another article How to: Sort a GridView Column When a Header Is Clicked that shows this for WPF, which looks conceptually very similar. I need to test it out to see if it handles non-string types.

#

The reason I pose this question is that I'm looking at WPF development and so using the latest version seems sensible. However, this means rolling out the .NET 4 runtime to PCs on old versions of the framework.

Windows XP is still the number one O/S (estimated 40%+ market share). To run .NET 4 on XP requires Service Pack 3, and although it is good practice to move to the latest service packs, often large companies are slow to keep up due to the extensive testing involved.

In fact, .NET 4 is not installed as standard with any Windows O/S as yet - Windows 7 and 2008 Server R2 have 3.5 installed.

This is not quite as big an issue as it was for .NET 3.5 as .NET 4 is significantly smaller as it doesn't include the older runtimes - .NET 3.5 SP1 included .NET 3 and .NET 2 and was 250MB, although this was reduced by doing a web install.

The size is also reduced a bit if you target the .NET 4 Client Profile, which should be OK for many WPF applications, and I think this may be rolled out as part of Windows service packs soon.

But still, if your application is only 4-5 MB and you need 40-50 MB of Framework it is worth consideration before jumping in and using the new shiny features.

#

T-SQL has never been my favorite language, but I need to use it on a fairly regular basis and every time I seem to Google the same things. So if I add it here, it might help others with the same issues, but it will also save me time later as I will know where to look for the answers!!

1. How do I SELECT FROM WHERE to filter on a DateTime column?

As it happens this is easy but I always forget. You just put the DATE value in single quotes and in standard format:

SELECT StartDate FROM Customer WHERE StartDate >= '2011-01-01' ORDER BY StartDate

2. How do I then GROUP BY and get a count by StartDate?

Bit trickier, but you can use the built in DATEADD and DATEDIFF to set the TIME part to midnight, allowing the GROUP BY to have a consistent value to work on:

SELECT DATEADD (d, DATEDIFF(d, 0, StartDate),0) [Customer Creation Date], COUNT(*) [Number Of New Customers]
FROM Customer
WHERE StartDate >= '2011-01-01'
GROUP BY DATEADD(d, DATEDIFF(d, 0, StartDate),0)
ORDER BY [Customer Creation Date]

Note: [Customer Creation Date] and [Number Of New Customers] column alias just provide more readable column headers.

3. Finally, how can you format the DATETIME to only show the DATE part (after all the TIME part is now always midnight)?

The built in CONVERT function allows you to convert the DATETIME to a CHAR array using a specific format. The format is a bit arbitrary and needs looking up, but 101 is the U.S. standard mm/dd/yyyy, and 103 is the U.K. standard dd/mm/yyyy.

SELECT CONVERT(CHAR(10), DATEADD(d, DATEDIFF(d, 0, StartDate),0), 103) [Customer Creation Date], COUNT(*) [Number Of New Customers]
FROM Customer
WHERE StartDate >= '2011-01-01'
GROUP BY DATEADD(d, DATEDIFF(d, 0, StartDate),0)
ORDER BY [Customer Creation Date]

 #

 

When I found out yesterday that one of my top 5 development tools .NET Reflector will no longer be free at the end of February, I thought I'd see if work had started on a good open source alternative...and guess what...work on ILSpy is already well underway!!

There seems to be a difference of opinion on what Red Gate said when they purchased .NET Reflector from Lutz Roeder in 2008. They say that they would try to keep it free, where as others think they promised to keep it free. Either way at the time I thought it was a smart purchase by Red Gate as it would raise their profile overnight within the .Net community.

But not only are they going to charge $35 for v7 (which is up to them), they have also time-bombed v6 to force users to pay. This I think will lower their profile overnight within the .Net community!!

Maybe they are been slightly naive in thinking the community wouldn't just write an alternative?

UPDATE: JetBrains now has a free decompiler called dotPeek:
http://www.jetbrains.com/decompiler/

Features:

  1. Decompiling .NET 1.0-4.5 assemblies to C#
  2. Support for .dll, .exe, .zip, .vsix, .nupkg, and .winmd files
  3. Quick jump to a type, assembly, symbol, or type member
  4. Effortless navigation to symbol declarations,
    implementations, derived and base symbols, and more
  5. Accurate search for symbol usages
    with advanced presentation of search results
  6. Overview of inheritance chains
  7. Support for downloading code from source servers
  8. Syntax highlighting
  9. Complete keyboard support
  10. dotPeek is free!

#

 

When applying to join Geeks with Blogs I had to specify the development tools I use every day. That got me thinking, it's taken a long time to whittle my tools of choice down to the selection I use, so it might be worth sharing.

Before I begin, I appreciate we all have our preferred development tools, but these are the ones that work for me.

Microsoft Visual Studio

Microsoft Visual Studio has been my development tool of choice for more years than I care to remember. I first used this when it was Visual C++ 1.5 (hats off to those who started on 1.0) and by 2.2 it had everything I needed from a C++ IDE. Versions 4 and 5 followed and if I had to guess I would expect more Windows applications are written in VC++ 6 and VB6 than any other language.

Then came the not so great versions Visual Studio .Net 2002 (7.0) and 2003 (7.1). If I'm honest I was still using v6. 2005 was better and 2008 was simply brilliant. Everything worked, the compiler was super fast and I was happy again...then came 2010...oh dear.

2010 is a big step backwards for me. It's not encouraging for my upcoming WPF exploits that 2010 is fronted in WPF technology, with the forever growing Find/Replace dialog, the issues with C++ intellisense, and the buggy debugger. That said it is still my tool of choice but I hope they sort the issue in SP1.

I've tried other IDEs like Visual Age and Eclipse, but for me Visual Studio is the best. A really great tool.

Liquid XML Studio

XML development is a tricky business. The W3C standards are often difficult to get to the bottom of so it's great to have a graphical tool to help. I first used Liquid Technologies 5 or 6 years back when I needed to process XML data in C++. Their excellent XML Data Binding tool has an easy to use Wizard UI (as compared to Castor or JAXB command line tools) and allows you to generate code from an XML Schema. So instead of having to deal with untyped nodes like with a DOM parser, instead you get an Object Model providing a custom API in C++, C#, VB etc.

More recently they developed a graphical XML IDE with XML Editor, XSLT, XQuery debugger and other XML tools. So now I can develop an XML Schema graphically, click a button to generate a Sample XML document, and click another button to run the Wizard to generate code including a Sample Application that will then load my Sample XML document into the generated object model.

This is a very cool toolset.

Note: XML Data Binding is nothing to do with WPF Data Binding, but I hope to cover both in more detail another time.

.Net Reflector

Note: I've just noticed that starting form the end of February 2011 this will no longer be a free tool !!

.Net Reflector turns .Net byte code back into C# source code. But how can it work this magic? Well the clue is in the name, it uses reflection to inspect a compiled .Net assembly. The assembly is compiled to byte code, it doesn't get compiled to native machine code until its needed using a just-in-time (JIT) compiler. The byte code still has all of the information needed to see classes, variables, methods and properties, so reflector gathers this information and puts it in a handy tree.

I have used .Net Reflector for years in order to understand what the .Net Framework is doing as it sometimes has undocumented, quirky features. This really has been invaluable in certain instances and I cannot praise enough kudos on the original developer Lutz Roeder.

Smart Assembly

In order to stop nosy geeks looking at our code using a tool like .Net Reflector, we need to obfuscate (mess up) the byte code. Smart Assembly is a tool that does this. Again I have used this for a long time. It is very quick and easy to use.

Another excellent tool.

Coincidentally, .Net Reflector and Smart Assembly are now both owned by Red Gate. Again kudos goes to the original developer Jean-Sebastien Lange.

TortoiseSVN

SVN (Apache Subversion) is a Source Control System developed as an open source project. TortoiseSVN is a graphical UI wrapper over SVN that hooks into Windows Explorer to enable files to be Updated, Committed, Merged etc. from the right click menu.

This is an essential tool for keeping my hard work safe! Many years ago I used Microsoft Source Safe and I disliked CVS type systems. But TortoiseSVN is simply the best source control tool I have ever used.

---

So there you have it, my top 5 development tools that I use (nearly) every day and have helped to make my working life a little easier. I'm sure there are other great tools that I wish I used but have never heard of, but if you have not used any of the above, I would suggest you check them out as they are all very, very cool products.

#

 

Hi and welcome!

I'm a software developer and this is my geeks blog. I have 20 years Visual Studio mainly C++, MFC,  ATL and now, thankfully, C# and I am embarking on the new world (well new to me) of WPF, so I thought I would try and capture my successful...and not so successful...WPF experiences with the geek world.

So where to start? WPF?

What I know so far...

From wiki..."Windows Presentation Foundation (or WPF) is a graphical subsystem for rendering user interfaces in Windows-based applications."

Hmm, great but didn't MFC, ATL (my head hurt with that one), and .Net all have APIs to allow me to code against the Windows Graphical Device Interface (GDI)?

"Rather than relying on the older GDI subsystem, WPF utilizes DirectX. WPF attempts to provide a consistent programming model for building applications and provides a separation between the user interface and the business logic."

OK, different drawing code, same Windows and weren't we always taught to separate our UI, Business Layer and Data Access Layer?

"WPF employs XAML, a derivative of XML, to define and link various UI elements. WPF applications can be deployed as standalone desktop programs, or hosted as an embedded object in a website."

Cool, now we're getting somewhere. So when they say separation they really mean separation. The crux of this appears to be that you can have creative people writing the UI and making it attractive and intuitive to use, whist the geeks concentrate on writing the Business and Data Access stuff.

XAML (eXtensible Application Markup Language) maps XML elements and attributes directly to Common Language Runtime (CLR) object instances, properties and events. True separation of the View and Model.

WPF also provides logical separation of a control from its appearance. In a traditional Windows system, all Controls have a base class containing a Windows handle and each Control knows how to render itself. In WPF, the controls are more like those in a Web Browser using Cascading Style Sheet, they are not wrappers for standard Windows Controls. Instead, they have a default 'template' that defines a visual theme which can easily be replaced by a custom template.

But it gets better. WPF concentrates heavily on Data Binding where the client can bind directly to data on the server. I think this concept was first introduced in 'Classic' Visual Basic, where you could bind a list directly to a data from an Access database, and you could do similar in ASP .Net. However, the WPF implementation is far superior than it's predecessors.

There are also other technologies that I want to look at like LINQ and the Entity Framework, but that's all for now.

#