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
165 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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