Skip to main content

Formatting TFN and ABN

I'm not entirely sure if I've mentioned this before BUT I happen to LOVE extension methods.

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

I recently had to write some of them up for Formating TFN and ABNs.

Here are the TfnAbnExtnesion Methods. (I could have also used regular expression to accomplish this but I chose to keep it simple/maintainable.)
 
public static class TfnAbnExtensions
{
/// <summary>
/// Formats the TFN.
/// If the length is 9 char, return XXX XXX XXX
/// If the length is 8 char, return XXX XXX XX
/// </summary>
/// <param name="tfn">The TFN.</param>
/// <returns></returns>
public static string FormatTfn(this string tfn)
{
// Ignore null/empty strings
if (string.IsNullOrWhiteSpace(tfn))
{
return string.Empty;
}

// Remove any existing whitespaces
var newtfn = tfn.UnFormatAbnTfn();

var result = string.Empty;
var tfnCharArray = newtfn.ToCharArray();

for (int i = 0; i < tfnCharArray.Length; i++)
{
if (i % 3 == 0 && i != 0)
{
result += " ";
}

result += tfnCharArray[i].ToString(CultureInfo.InvariantCulture);
}

return result;
}


/// <summary>
/// Formats the ABN.
/// </summary>
/// <param name="abn">The ABN.</param>
/// <returns></returns>
public static string FormatAbn(this string abn)
{
// Ignore null/empty strings
if (string.IsNullOrWhiteSpace(abn))
{
return string.Empty;
}

// Un-format the ABN
var newtfn = abn.UnFormatAbnTfn();

var result = string.Empty;
var abnCharArray = newtfn.ToCharArray();
var tempCounter = 0;

for (int i = 0; i < abnCharArray.Length; i++)
{
// Second character & then every 3rd char
if (i == 2 || tempCounter == 3)
{
result += " ";
tempCounter = 0;
}

tempCounter++;
result += abnCharArray[i].ToString(CultureInfo.InvariantCulture);
}

return result;
}


/// <summary>
/// Un formats the ABN or TFN by removing blank spaces
/// </summary>
/// <param name="abnTfn">The ABN or TFN.</param>
/// <returns></returns>
public static string UnFormatAbnTfn(this string abnTfn)
{
return string.IsNullOrWhiteSpace(abnTfn) ? abnTfn :
abnTfn
.Replace(" ", string.Empty)
.Replace("-", string.Empty)
.Replace("/", string.Empty);
}

}

References: Extension Methods (C# Programming Guide)

Comments

Popular posts from this blog

Internet Information Services(IIS) reveals its real or internal IP Address

In the ever changing world of global data communications, inexpensive Internet connections, and fast-paced software development, security is becoming more and more of an issue. Security is now a basic requirement because global computing is inherently insecure.

Keeping that in mind, we recently ran our flagship product through a security audit. It was such a helpful exercise in tying-off any remaining lose ends in our application in terms of application security. 
Based on the security audit report, there was a relatively minor issue that appeared when accessing the /images directory of our application. Turns out that the Location response header of the 301 request returns an Internal IP address. The issue is detailed below.

Issue reportedInternet Information Services (IIS) may reveal its real or internal IP address in the Location header via a request to the /images directory. The value returned whilst pen testing is https://10.0.0.10/images.

The riskInformation regarding internal IP add…

IIS Request Filtering to block HTTP Verbs (For example Trace)

The issueRequest Filtering is a built-in security feature that was introduced in Internet Information Services (IIS) 7.0. This can be used to block specific verbs like "Trace".

When request filtering blocks an HTTP request, IIS 7 will return an HTTP 404 error to the client and log the HTTP status with a unique substatus that identifies the reason that the request was denied. Verb Denied.

HTTP SubstatusDescription404.5URL Sequence Denied404.6Verb Denied404.7File Extension Denied404.8Hidden Namespace404.1Request Header Too Long404.11URL Double Escaped404.12URL Has High Bit Chars404.13Content Length Too Large

Unit Testing HttpContext.Current.Session in MVC3 .NET

We recently changed some functionality where during the "CREATE" process, we go through a wizard to save application data. This data is saved only to the session in the final step when the user clicks the final submit.

This was easy enough to implement but when I started writing unit tests for my static methods that Add, Update, Delete or Modify the contents of our application data in the session, I got the following error:
System.NullReferenceException: Object reference not set to an instance of an object.

Turns out I had forgotten to setup the HttpContext.
The following "TestInitialise" method fixed my problem :)

[TestInitialize]
public void TestSetup()
{
// We need to setup the Current HTTP Context as follows:

// Step 1: Setup the HTTP Request
var httpRequest = new HttpRequest("", "http://localhost/", "");

// Step 2: Setup the HTTP Response
var httpResponce = new HttpResponse(new StringWriter());

// Step 3: Se…