-
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#?
-
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...
-
Thanks vasanth
That code snippet was just what I was loking for.
-
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...
-
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.
-
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
-
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....
-
Ooooo!!! Look at my code!!! :D
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();
}
}
}