Developing an XML Web Service Using Visual Studio 2005 - Developing the XML Web Service
(Page 4 of 6 )
Before proceeding further, you need to remove (or comment) all the statements related to the “HelloWorld” method (as it is intended only for demonstration).
Start with a new method “NumberToWords” as following:
[WebMethod(Description = "Returns the given number in words.")]
public string NumberToWords(long Number)
{
string sRetVal = "";
try
{
sRetVal = ConvertToWords(Number);
}
catch (Exception ex)
{
return String.Format("Error: {0}", ex.Message);
}
return sRetVal;
}
You should observe that the “NumberToWords” method does not process anything at the moment (apart from accepting a “long” number as parameter). The processing starts with the “ConvertToWords” method. Now, let us proceed with the “ConvertToWords” method as following:
private string ConvertToWords(long lValue)
{
//If the give number excides 999 trillion then raise an exception.
if (lValue.ToString().Length > 15)
throw new Exception("Excides maximum limit. This Service can translate up to 999 trillion.");
string sInWords = "";
string sSource = lValue.ToString();
string sStirngToProcess = "", sProcessedString = "";
string[] arrPalce = { "", " Thousand ", " Million ", " Billion ", " Trillion " };
int nCount = 0;
while (sSource.Trim().Length > 0)
{
//Take last 3 digits from the given number
if (sSource.Length > 3)
{
sStirngToProcess = sSource.Substring(sSource.Length - 3);
sSource = sSource.Substring(0, sSource.Length - 3);
}
else
{
sStirngToProcess = sSource;
sSource = "";
}
//Process last 3 digits
sProcessedString = Process3DigitNumber(sStirngToProcess);
if (sProcessedString.Trim().Length > 0)
sInWords = sProcessedString + arrPalce[nCount] +
sInWords;
nCount++;
}
if (sInWords.Trim().Length <= 0)
sInWords = "Zero";
return sInWords;
}
You could observe from the above code that the number gets divided into several chunks which are later processed by the “Process3DigitNumber” method. Now, let us go through the implementation details of “Process3DigitNumber”, “Process2DigitNumber” and “ProcessSingleDigitNumber” in the next section.
Next: Developing the XML Web Service continued >>
More Visual Basic Articles
More By Jagadish Chaterjee