Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2012, 02:41 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code validation

HI everyoone
I'm new in C# and i have one question. I should write a code that validates a text that should be: 7 digits in 6 rows,and should be separated by a space.Like this
1 2 3 4 5 6 7
0 8 9 7 1 3 9
4 5 6 9 7 2 1
6 9 2 0 5 6 6
.
.
.
I'm writting the code in Visual Studio 2008, windowsformapplication.
I have no idea how to write this, so plese help :) Thanks
 
Old January 4th, 2012, 04:49 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Validation

Hi Isabella,

You need to think about how you are going to get the data to be valiated into your program. Will it be manually input?, read from a file? or a database? Or it could be hard coded into the program.

It sounds like you created a new project from the Windows Form Application template. This will give you more than you need, but it also provide a way that you could add user input to the form to get your data.

The simpliest thing to do might be to add a button to your form. Double clicking the button in the design view will create a function to respond to a click of the button when running. You can put your validation code in this function.

From your description it sounds like you will be getting a series of strings. So you might be putting the data in an array as in

Code:
string[] MyData = new string[4];

MyData[0] = "1 2 3 4 5 6 7";
...
or a collection such as an ArrayList:

Code:
ArrayList MyData = new ArrayList();

MyData.Add("1 2 3 4 5 6 7");
...

Your code will need to parse each of the strings into individual digits.
You can take advantage of member functions of the string class like "Split" to do this.

You haven't specified what about the data to validate so I'll leave that up to you.

Hopefully, I've give you enough to get started. Come back with more questions when you have more done.
 
Old January 4th, 2012, 05:32 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
This is manually input by a user. The user should enter the values(0-9, in 6 rows, separated by space) in a textBox and after he clicks on a button it should verify the input like I mentioned above. If it's wrong than it should pop up an error message. I know that this code should be in btn_Click function, but I I don't how to verify the input. Should I use the if statement or maybe the for loop? Thanks :)
 
Old January 4th, 2012, 06:01 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Validation

A TextBox control will contain an array of Lines if you allow more than one line in the TextBox. You need to itterate through the Lines array to get each string the user entered. You can use the "for" statement to do this.

You could use another "for" statement to parse each string, checking each character for either a space or a single digit integer.

If you are not familiar with how to use a "for" statement in C# you should look that up.

To determine if a single character is an integer you should read about the member functions of the Integer class.

You might get started here:
http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding validation code in excel donsls Excel VBA 1 November 1st, 2008 01:37 AM
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
running example code from Ch 3 - Form Validation cshu BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 November 4th, 2006 05:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.