Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
172 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
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

971 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