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
161 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 29

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now