Wednesday, April 15, 2009

Australian Postcode Validation

I was recently porting and updating some old Pascal postcode validation to C# and thought i'd stick it up here.

To tell the truth, the Pascal code is more concise. Pascal, VB, Ruby etc have means to test a range of values in a single case statement. Unfortunately C# doesn't provide for this contingency, hence the "fall through's" in the case blocks.

Anyway, maybe someone might find this useful as there are a few "interesting" situations with the ACT and NSW.



 1     public static string GetState(string postCode) {
 2       if (!IsValidPostCodeFormat(postCode))
 3         return "";
 4
 5       switch (postCode[0]) {
 6         case '5': return "SA";             // SA:  5000-5999
 7         case '3':
 8         case '8': return "VIC";            // VIC: 3000-3999, 8000-8999
 9         case '4':
10         case '9': return "QLD";            // QLD: 4000-4999, 9000-9999
11         case '6': return "WA";             // WA:  6000-6999
12         case '7': return "TAS";            // TAS: 7000-7999
13         case '1': return "NSW";            // NSW: 1000-1999
14
15         case '0':
16           switch (postCode[1]) {
17             case '8':
18             case '9': return "NT";         // NT:  0800-0999
19             case '2': return "ACT";        // ACT: 0200-0299
20             default: return "";
21           }
22
23         case '2':
24           int tmp = Int32.Parse(postCode);
25           if ((tmp >= 2600 && tmp <= 2618) || (tmp >= 2900 && tmp <= 2920))
26             return "ACT";                  // ACT: 2600-2618, 2900-2920
27           else if ((tmp >= 2000 && tmp <= 2599) || (tmp >= 2619 && tmp <= 2898)
28                                                 || (tmp >= 2921 && tmp <= 2999))
29             return "NSW";                  // NSW: 2000-2599, 2619-2898, 2921-2999
30           else return "";
31
32         default: return "";
33       }
34     }
35
36     private static bool IsValidPostCodeFormat(string postCode) {
37       int tmp;
38       if (String.IsNullOrEmpty(postCode) || postCode.Length != 4 
39                                          || !Int32.TryParse(postCode, out tmp))
40         return false;
41
42       return true;
43     }
44




Monday, December 29, 2008

System.RunTime.Serialization.SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are: 3C-68-74-6D-

System.RunTime.Serialization.SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are: 3C-68-74-6D-6C-3E-0D-0A-20-20-20-20-3C-68-65-61-64 ...

This is the Remoting exception IIS was consistently giving me after a deep search. The search was returning some mammoth blob's and failing after a few minutes.

I thought it might be an IIS error, sure enough after firing up TCPTrace and sniffing around, I found


HTTP/1.1 100 Continue
Server: ASP.NET Development Server/9.0.0.0
Date: Sun, 28 Dec 2008 23:52:31 GMT
Content-Length: 0

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/9.0.0.0
Date: Sun, 28 Dec 2008 23:54:35 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 2549
Connection: Close

--snip html
Request timed out.
--snip html

So rather than throwing a relevant HTTP response code, we get a html error page that can't be serialized. Hopefully this saves fellow insane technologists a little time.

Wednesday, September 24, 2008

Returning lambda's from a ternary in C#

In the midst of some mad refactoring I came across something similar to the following snippet:

 1 if (count == 0)
 2   choice.Finish = result => { return; };
 3 else
 4   choice.Finish = result => { letter.MailMerge(result); };

Looked like a decent candidate for refactoring with a ternary / conditional operator:

 7 choice.Finish = (count == 0) ? result => { return; }
 8                              : result => { letter.MailMerge(result); };

Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'

Hold your horses, say what?

For some reason the only way to fool it through the compiler is to cast one of the expressions (it doesn't seem to matter which one) to the expected return type.

11 choice.Finish = (count == 0) ? (TypeDelegate<int>) (result => { return; })
12                              : result => { letter.MailMerge(result); };


Go figure!

Tuesday, September 2, 2008

VIM on OpenVMS

This is a note to future Dan and anyone else that may have file permission problems when running VIM on OpenVMS.

Define this logical:

Dan$ define DECC$ACL_ACCESS_CHECK ENABLE

Apparently when disabled the access function only checks the UIC protection and not the OpenVMS ACL's.

There are all other kinds of goodies here

Monday, August 11, 2008

SVN 1.5.1 / Ruby bindings on Windows

Rather than messing around with compiling swig bindings etc:

  1. Download the ruby bindings from the svn site: (http://subversion.tigris.org/files/documents/15/43245/svn-win32-1.5.1_rb.zip)

  2. From the zip - copy ruby\lib\svn into c:\ruby\lib\ruby\site_ruby\1.8\svn

  3. From the zip – copy ruby\ext\svn\ext into c:\ruby\lib\ruby\site_ruby\1.8\svn\ext

  4. copy libeay32.dll and ssleay32.dll from your subversion 1.5 directory into c:\ruby\bin

  5. run irb and test with: require ‘svn/core’

Saturday, June 14, 2008

Why?

css:

.mainMenuHeader {color:#FFFFFF;}
.mainMenuHeader:hover {color:#F26540;}

xaml:

<Style x:Key="mainMenuHeader">
  <Setter Property="Control.Foreground" Value="#FFFFFF" />
  <Style.Triggers>
    <Trigger Property="Control.IsMouseOver" Value="True">
      <Setter Property="Control.Foreground" Value="#F26540" />
    </Trigger>
  </Style.Triggers>
</Style>

Saturday, May 31, 2008

Home Town Hero - That's How I Know

Hopping back in the time machine to 2002 a slightly rockin' album came out - Home Town Hero - Self Titled. It sounds a little grungy like The Exies and a little power-poppy like Rooney.

They had some minor successes opening for STP, Incubus and Weezer. Since then, they have completely fallen off the radar. Apparently there was some unpleasantness with their record label and they are now back under the moniker: Under the Influence of Giants. I gave their new LP a listen, i just couldn't get into it. I think it has validly been classified as disco-rock :)

Nick and I were giving it a thrash this week, so as an ode to the good old days I tabbed out the secret/bonus/acoustico track, as it appears no-one else has.

It ain't the most complicated tune on the planet but stay tuned for Niccolò Paganini's 24th Caprice next week :)

bon appétit!

Begin Tab
Standard tuning (EADGBE)
Intro (let ring)

 C       G\B      Am       F           C       G\B     C
---------------------------------------------------------------------
---------------------------------------------------------------------
-----0-------0--------2--------------------0-------0---------0---0---
---2---2---0---0----2---2------3---------2---2---0---0---2---2---2---
-3-------2--------0----------3---3-----3-------2-------3---3-3-3-3---
---------------------------1-----------------------------------------


Verse

 C       G\B      Am       F         C       G\B     E       F
------------------------------------------------------------------------
------------------------------------------------------------------------
-----0-------0--------2------------------0-------0----------------------
---2---2---0---0----2---2------3-------2---2---0---0-----2-------3------
-3-------2--------0----------3---3---3-------2---------2---2---3---3----
---------------------------1-------------------------0-------1----------

 C       G\B     Am        F    
-----------------------------------------
-----------------------------------------
-----0-------0-------2-------------------
---2---2---0---0---2---2-------3---------
-3-------2-------0-----------3---3-------
---------------------------1-------------

 C       G\B      E          F
-----------------------------------------
-----------------------------------------
-----0-------0----1-1-1-x-x--2-2--x-x-x---
---2---2---0---0--2-2-2-x-x--3-3--x-x-x---
-3-------2--------2-2-2-x-x--3-3--x-x-x---
------------------0-0-0-x-x--1-1--x-x-x---


Chorus (just bang out the chords instead of picking them)

 C   G\B  Am  F  C  G\B  C    (into to bridge)  
-0---3--------1--0--3----0--|--0------------
-1---3----1---1--1--3----1--|--1------------
-0---0----2---2--0--0----0--|--0------------
-2---0----2---3--2--0----2--|--0------------
-3---2----0---3--3--2----3--|--2------------
--------------1-------------|---------------


Bridge

 Am  F   G\B  Am  C  E    Am   F   G\B   Am  C   B\G
-0---1---3----0---0--0----0----1---3-----0---0---3---
-1---1---3----1---1--0----1----1---3-----1---1---3---
-2---2---0----2---0--1----2----2---0-----2---0---0---
-2---3---0----2---2--2----2----3---0-----2---2---0---
-0---3---2----0---3--2----0----3---2-----0---3---2---
-----1---------------0---------1---------------------