Hi Guys
NP93 Error in Program
I got this program from a website. Website address is given below. This program is not executing. Anyone knows please correct the error.
Thank you
//http://msdn2.microsoft.com/en-us/library/system.iformatprovider.aspx
using System;
public class TestFormatting
{
public static void
{
long acctNumber;
acctNumber = 104254567890;
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0:H}", acctNumber));
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0}", acctNumber));
acctNumber = 14567890;
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0:H}", acctNumber));
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0}", acctNumber));
acctNumber = 18779887654111;
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0:H}", acctNumber));
Console.WriteLine(String.Format(new AcctNumberFormat(), "{0}", acctNumber));
}
}
// The example displays the following output to the console:
// 10425-456-7890
// 104254567890
// 00001-456-7890
// 000014567890
// 18779-887-6541
// 187798876541
Posted Apr 6, 2008, 11:15 AM
Thank you for your help, Scott Lysle
Scott LyslePosted Apr 6, 2008, 10:56 AM
You need the rest of the example if you did not get it:
public class AcctNumberFormat : IFormatProvider, ICustomFormatter
{
private const int ACCT_LENGTH = 12;
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string fmt, object arg, IFormatProvider formatProvider)
{
// Convert argument to a string
string result = arg.ToString();
// If account number is less than 12 characters, pad with leading zeroes
if (result.Length < ACCT_LENGTH)
result = result.PadLeft(ACCT_LENGTH, '0');
// If account number is more than 12 characters, truncate to 12 characters
if (result.Length > ACCT_LENGTH)
result = result.Substring(0, ACCT_LENGTH);
// Add hyphens for formatting code "H"
if (! String.IsNullOrEmpty(fmt) && fmt.ToUpper() == "H")
return result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8);
// Return string representation of argument for any other formatting code
else
return result;
}
}