Home » Blog

rounded header

Archive for August, 2010

String formatting in WPF and Silverlight

tag icon Tagged as General, Silverlight, WPF

If you’re building controls or applications that display numbers and dates, you’re going to want to have a good understanding of how to format them nicely. In WPF and Silverlight, this is achieved using the static String.Format method which has a few overloads. The method overload we’ll be looking at takes in a string and an object as the parameters. The string parameter is a format string which will be used to format the given object. The object is whatever we want to convert to a string such as a double or a DateTime. In this blog post we will look at just a few of the many format strings provided by WPF and Silverlight for formating doubles and DateTimes. The general structure of a format string is: “{0:*}” where the * is replaced with the format string itself.

C# String format for double

Most format strings for double values are comprised of some numbers or symbols followed by a decimal point followed by some more numbers or symbols. The most common string format for formatting a double value is to specify the number of decimal places. As seen in the examples below, the number of following zeros define the exact number of decimal places to display. The number of “#” symbols can be used to define a maximum number of decimal places while only displaying digits if they are not zero. The last example below shows how “0.0#” can be used to specify that at least one decimal place is always shown, but only a maximum of two decimal places.

// exactly two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"
 
// at most two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
 
// one or two decimal places
String.Format("{0:0.0#}", 123.4567);      // "123.46"
String.Format("{0:0.0#}", 123.4);         // "123.4"
String.Format("{0:0.0#}", 123.0);         // "123.0"

C# String format for DateTime

Format strings for DateTime objects are built up as a sequence of grouped letters. Each letter targets a particular part of the DateTime such as the year, day or hour. The number of letters in each group defines how that part should be formatted. Below is a list of some of the letters and the results of applying them to a DateTime.

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 0);
 
String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"      year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"     month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday"    day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"         hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"               minute
String.Format("{0:s ss}",          dt);  // "7 07"               second
String.Format("{0:t tt}",          dt);  // "P PM"               AM or PM

By using any combination of these letter groups within a format string, you can display a DateTime in any possible way you need. Between each letter group, you may also want to include a symbol such as a comma, colon or slash. The slash and colon symbols are special characters known as the date separator and time separator respectively. One thing to keep in mind is that the date and time separator characters may be displayed differently depending on the current culture of the application. Month and day names will also be displayed in different languages based on the culture.

// DateTime format examples
 
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);   // "9/3/2008 16:05:07"

Format strings in XAML

If you ever come across a situation where you need to set a property of an object in XAML to be a format string, you may notice a small problem. Format strings such as the ones described in this blog post start and end with the curly bracket symbols. So the following code will fail to compile because the curly bracket is a special character which XAML will interpret in its own way rather than using it as a string.

<MyFormattingObject FormatString="{0:0.0}" />

To get around this issue, all we need to do is place an empty pair of curly brackets before the format string to tell XAML to interpret it as a string value.

<MyFormattingObject FormatString="{}{0:0.0}" />

More information

For a more extensive list of format string tips and tricks, you can follow this link to find DateTime formats, and check here to find string formats for doubles.

Nightly news, 27 August 2010

Well, that was August. Four releases in four weeks. Mindscape HQ has been a non-stop riot of launch parties and last-minute brow-furrowing attempts to think of amusing discount codes. Despite all that we still have few updates in the nightlies.

LightSpeed

  • Fixed an issue where auto through entity tables were not created during database sync if the source was a STI derived entity
  • Added a check for the Client Profile when creating a new LightSpeed model

WPF Diagramming

  • Fixed DiagramPrinter printing nodes whose elements had been set to invisible

We’d like to remind customers that most of the Mindscape crew will be at TechEd New Zealand for the first three days of next week and this is likely to impact forums support. Please be patient — we’ll respond to posts when we can.

SimpleDB Batching in LightSpeed 3.11

tag icon Tagged as LightSpeed

One of the enhancements made to LightSpeed 3.11 was to improve the efficiency of inserts when working with SimpleDB. If you’ve not heard of SimpleDB, it’s a cloud based database offering from Amazon. It’s not a traditional relational database, but rather a key/value store which is pretty neat if you’re into that sort of thing. We first released support for mapping over SimpleDB last year and got a great response from people wanting to explore SimpleDB. Nicely, LightSpeed is still the only .NET ORM that supports SimpleDB :-)

Storm clouds

One of the potential performance problems with cloud based databases is that queries take the form of a web request. As you’ll be well aware, these are an order of magnitude slower that calling a database running on your machine, or even on your same network segment. Unfortunately this means that if you wanted to push a heap of inserts into your database the SimpleDB provider would chug away making a call… getting a response… making the next call… getting a response… and so on. This latency made performance really quite slow for large data loading exercises.

Batched put attributes

Jeremy started investigating what could be done and found Amazon had enhanced the API for SimpleDB (and surfaced it through their .NET wrapper) to allow for “batched puts” which is SimpleDB lingo for batching of inserts. With a little bit of work on the core engine, LightSpeed can be configured to batch up inserts into collections of 25 – which is the limit that Amazon imposes on the batch size. This yields ~25x performance for inserts.

Turning on batching for SimpleDB

As this capability was not being released as part of a major version change we’ve left it to developers to enable this functionality. It’s pretty easy to do, just add the following to your connection string for SimpleDB providers:

Enable Batch Insert=true

More improvements planned

Currently batching is only enabled for the insert operations. Support for update operations is planned and then we will likely investigate some additional performance enhancements we can make to ensure that LightSpeed continues to earn it’s name. Also, a special mention must be made to our LightSpeed user “MiddleTommy” who has provided great feedback on working with LightSpeed which lead to this enhancement being made – thanks! :-)

Hope that helps all our SimpleDB users out there! Also, if you’re tinkering with SimpleDB, be sure to check out our SimpleDB Management Tools which gives a SQL Management Studio feel to working with SimpleDB directly from within Visual Studio.

How to avoid TeamCity errors while building Visual Studio extensions

tag icon Tagged as General

We love JetBrains’ TeamCity continuous integration server — dead easy to use, cool features like personal builds and for small setups like ours it’s free!

Unfortunately, if you’re building Visual Studio extensions, the latest version of TeamCity has a nasty sting in the tail: the Visual Studio integration component installs itself into the experimental instance and throws lots of spurious exceptions that throw you into the debugger whenever you close a document window or shut down the experimental instance. To add insult to injury, it also logs a first-chance exception every few seconds, swamping your own extension’s debug output. And you can’t remove the component through Add-In Manager or Extension Manager.

We do quite a bit of Visual Studio development, so this has been causing a lot of grief and despondency at Mindscape HQ. Fortunately, there is a way to stop the TeamCity add-in running, it’s just not very obvious. If you’re running into the same problem, here’s what you need to do:

  • Start the experimental instance
  • Go into Tools > Options
  • Select the TeamCity entry on the left
  • Click Suspend
  • Click OK

TeamCity will throw a couple more spurious exceptions for old times’ sake, but after that the errors should go away. Enjoy!

Mindscape at TechEd New Zealand

tag icon Tagged as News

The signs of spring are here — lambs frolicking in the fields, occasional glimpses of the sun, and of course TechEd New Zealand. As always, Mindscape will be there in force! We don’t have a stand this year, but you’ll be able to find us around the conference floor — or, in the case JD at the drinks evenings, on the conference floor* — or you can catch us at the following sessions:

  • From KLOCs to Ka-Ching – The Business of Software (Monday 11:50)
  • What’s New in C# 4 and Visual Basic 2010 (Monday 13:45)
  • Kentico, N2, Umbraco – CMS FTW! (Monday 13:45)
  • Bringing the Web to Life with jQuery (Tuesday 9:00)
  • Code Different (Tuesday 10:40)
  • ASP.NET MVC – What’s New and Cool in Versions 2 and 3 (Tuesday 14:55)
  • Parallelise Your Parallel Applications In Parallel With the Microsoft Parallel .NET Parallel Extensions for Parallelism (Tuesday 17:25)
  • LINQ Confidential – A Deep Dive Into How LINQ Works (Wednesday 11:50)

If you’re at TechEd, truck on up and say hello!

And while we’re talking conferences, don’t forget CodeCamp Auckland the day before TechEd — it’s not too late to register!

The serious bit

Since we’ll be pretty busy at TechEd, there’s likely to be some delay in responding to forums posts between Monday 30 August and Thursday 2 September. Please be patient — we’ll get back to you as soon as we can!

* JD has asked me to point out that this is a base slur. Last year, he reminds us, he was actually found in an Auckland pub trying to win a live crayfish to take home with him. Just so you don’t get the wrong impression.

Data Products Visual Controls Community Store
LightSpeed ORM
NHibernate Designer
SimpleDB Tools
SharePoint Tools
WPF Elements
WPF Diagrams
Silverlight Elements
Forums
Blog
Register
Login
Subscribe to newsletter
Buy Now
My Account
Volume Discounts
Purchase Orders
Contact Us