Thread: Validating input

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    589

    Validating input

    Hi

    I been using all kinds of cumbersome loops in combination with "Char.IsDigit" to validate my textbox user input to make sure it is valid but there gotta be a better way to do it. Does anyway know about the "right" way to check for a numerical input in C#?

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Ok you can do this...


    to convert a string to number do the following..



    for example

    string num="1234546";

    int number=int.parse(num);

    this will convert the string num to a numeric value and will store it into the integer variable number.

    THis will work in case the above string is not a number say it has some characters or something.. it will throw an Exception... Cath this execption and handle it... This is the easiest way... and recomended as it is actulally an Execption that is to be handled


    Code:
    string s="some string or numebr ";
    try
      {
        
     int num=int.parse(s);
       }
    
    
    catch
     {
    
      Concole.Write("The input is not a valid number ");
    
     }

    The execption thrown above is an "unexpected execption".. May be there is a better way of doing it... But i prefer handling it using try and catch block because it is convient.. and more flexible...
    Last edited by vasanth; 02-27-2003 at 03:22 AM.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Thanks vasanth

    That code snippet was just what I was loking for.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You are welcome.. Looks like this board is almost empty.. Not much people to solve dounts.. I think we should get involved here more... On various topics...

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    This is what I ended up with

    Code:
    private void tXPos_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        try
        {
            XPos = double.Parse(tXPos.Text);
        }
        catch(FormatException)
        {
            tXPos.Text = XPos.ToString();
            MessageBox.Show("X Input is not a valid number", "Invalid input in X", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch(OverflowException)
        {
            tXPos.Text = XPos.ToString();
            MessageBox.Show("X Input is out of range", "Invalid input in X", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch(Exception ex)
        {
            tXPos.Text = XPos.ToString();
            MessageBox.Show("" + ex, "Invalid input in X", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    One of the biggest reason I prefere C# is because the catch try system for all error handling.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well even C++ and JAVA has Execption handling...

    Well any way.. what was that code for.... And you are the only one here who seem to have been realy workin on C#.. it would be great if you could answer some of the questions on C# i have posted on the C# board..

    Thanx

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    True both Java and C++ have exception but I have never seen it been used concistently(sp). It is like everyone have there own way to return errors...and I am stil wery new to C#. Although it is so easy to make guis and syntax similar to C, C++, Java etc....

  8. #8
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Ooooo!!! Look at my code!!!

    This takes the RSS feed from my blog and parses it out to the console.

    Code:
    using System;
    using System.Xml;
    
    namespace MyBlogRssFeed
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			// Declare an xmldoc object and instantiate it
    			XmlDocument xmlDoc = new XmlDocument();
    
    			// Load the file into memory
    			xmlDoc.Load("http://dotnetweblogs.com/Cstewart/Rss.aspx");
    
    			// Load up a list of nodes by the given tag name
    			XmlNodeList items = xmlDoc.GetElementsByTagName("item");
    
    			// For every node in the items node list that has the value of value, 
    			// go into this logic and pull out certain pieces of info.
    			foreach(XmlNode item in items) 
    			{
    				Console.WriteLine("Title: {0}", item["title"].InnerXml);
    				Console.WriteLine("Link: {0}", item["link"].InnerXml);
    				Console.WriteLine("Date: {0}", item["pubDate"].InnerXml);
    				Console.WriteLine("Content: {0} \n", item["description"].InnerXml);
    			}
    
    			// Wait for the user to manually exit the application
    			Console.Read();
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Validating Monetary Input
    By Hexxx in forum C++ Programming
    Replies: 7
    Last Post: 02-01-2006, 08:27 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM