Solved

Within a C# console application how would you validate the length of a field read from an input file?

Posted on 2011-09-20
2
166 Views
Last Modified: 2013-12-17
I am developing a C# console application using Visual Studio 2005.

How would you modify the following IF statement in the code section that follows to validate that the field length of item_sequence_number is at least 8 characters in length and if it is not, generate an error message stating the field length is less than 8 characters?


if (reader.Name == "csc:item_sequence_number")
                        {
                            arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
                            itemsequence = reader.ReadElementContentAsString();
                            String formattedString = itemsequence.Substring(7, 8);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CpcsNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                                iCheckNumber--;
                            }
                        }

Open in new window

0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 36570720
Something like:
if (reader.Name.length >= 8)
{
 ...
}
else
{
   // generate error or print error message:
   Console.WriteLine("Name has to be at least 8 characters long");

}

Open in new window

0
 
LVL 2

Expert Comment

by:yogsoft
ID: 36571465
Somethinsg like below:

 
if (reader.Name == "csc:item_sequence_number")
            {
                arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
                itemsequence = reader.ReadElementContentAsString();

                if (itemsequence.Length >= 8)
                {
                    String formattedString = itemsequence.Substring(7, 8);
                    Int64 intTest = 0;

                    if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                        arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                    else
                    {
                        arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                        sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CpcsNo" + "," +
                                      "GROUP_FIELD_VALUE:" + formattedString);
                        iCheckNumber--;
                    }
                }
                else
                {
                    Console.WriteLine("Content string should be at least 8 charaters.");
                }
            }

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question