Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
169 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 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 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