question on C# and exception

akohan
akohan used Ask the Experts™
on


Hello group,

I'm reading some input file (text) where I have to parse and tokenzie some data. In order to extract a field such as
applicant-id I have used:

       trans.applicant_id = arData[x].Substring(0, 10);
        
which works fine as long as there is a valid value for applicant id in the text file like:

application id 0123456789

however, if I receive file without the value then I will run into exception error since I have put the code in a try/catch block.

What should I do (using if statement) in order not to get into exception when value doesn't exist.

Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
you mean:
if (arData[x] != string.Empty)
{
       trans.applicant_id = arData[x].Substring(0, 10);
} 

or do you want to tryparse to numerical data type?

Open in new window

try this
if(arData[x].Length >= 10)
{
    trans.applicant_id = arData[x].Substring(0, 10);
}

Open in new window

if(arData[x].Length >= 10)
{
    trans.applicant_id = arData[x].Substring(0, 10);
}
else
{
   trans.applicant_id = arData[x];
}
Commented:
As i understand, what you are asking for is a safe way to handle if the string you are parsing is null or less the length of what you expect. In this case you just wish to have the value of the content it self put into your result string.

.NET 3.5 provide you with extensions method where you can actually produce this type of robust functions in a general reuable way.

Below code snipper shows two block of code. the first part is the extension method, handling the need greedy details of null pointer and length validation, the second block, use this extension method to make a safe call returning the desired string, directly on the string in your array.

A couple of details is interesting here  

1) the nice thing about this solutions is that is is generic. You can use this safe method for anywhere in your code You just need in include the using My.Extensions namespace block in you source.

2) the extensions method (Substr) can even be called on an object that is null. This is because extensions method is actually just a compiler trick. On compile time it will be transform into the natual static corresponding code construction.
namespace My.Extensions
{
    public static class Extensions
    {
        public static string Substr(this string input, int len) 
        {
            if (string.IsNullOrEmpty(input) || input.Length < len)
            {
                return input;
            }
            return input.Substring(0, len);
        }
    }
 
}
 
// Below using the Extension method
using My.Extensions;
namespace SomeOtherPlace
{
    public class Demo
    {
        public static void Method()
        {
            string[] ar = "somedatas  asdf asd sdfsdf".Split(' ');
            for (int i = 0; i < ar.Length; i++)
            {
                string safeString = ar[i].Substr(10);
            }
        }
    }
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial