VisualDeveloper Posted January 22, 2003 Posted January 22, 2003 (edited) In my simple program (console application) below, I am requesting two integer values and then simply using the AddNumbers() function to return the result. However, the lines of code "Num1 = Console.ReadLine();" and ""Num2 = Console.ReadLine();" produce a build error reporting that it cannot implicitly convert type 'string' to 'int'. using System; namespace Calculator { class CCalculator { [sTAThread] static void Main(string[] args) { //local variables int Num1; int Num2; //welcome message Console.WriteLine("Welcome to Calculator 1.0"); //request first number Console.Write("Number 1: "); Num1 = Console.ReadLine(); //request second number Console.Write("Number 2: "); Num2 = Console.ReadLine(); //display result Console.WriteLine("Total: " + AddNumbers(Num1, Num2)); //finish Console.WriteLine("Press any key to continue..."); Console.Read(); } static int AddNumbers(int Number1, int Number2) { //add numbers and return result return (Number1 + Number2); } } }Thanks in advance... Shuaib Edited January 22, 2003 by VisualDeveloper Quote Windows XP Professional, Visual Studio .NET Enterprise Architect, Dell Dimension 8100, 15" TFT Monitor, Intel Pentium 4 CPU 1700MHz, 40GB Hard Disk, nVidia GeForce 3, NEC CD-RW 16x, Samsung DVD 16x.
*Gurus* divil Posted January 22, 2003 *Gurus* Posted January 22, 2003 Num1 = int.Parse(Console.ReadLine()); Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted January 22, 2003 *Experts* Posted January 22, 2003 This would be a good time to learn what try...catch does as well (take a look at the help). -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
VisualDeveloper Posted January 22, 2003 Author Posted January 22, 2003 (edited) I've been looking into the so-called "Try...Catch" statement this afternoon! Is there any way my program below could be improved or optimized?using System; namespace Calculator { class CCalculator { [sTAThread] //application entry point static void Main(string[] args) { //local variables int Num1 = 0; int Num2 = 0; bool Num1OK = false; bool Num2OK = false; //welcome message Console.WriteLine("Welcome to Calculator 1.0"); while (Num1OK == false) { try { //request first number Console.Write("Number 1: "); Num1 = int.Parse(Console.ReadLine()); //input for first number is valid Num1OK = true; } catch (Exception E) { //display error information Console.WriteLine("Error: " + E.Message); } } while (Num2OK == false) { try { //request first number Console.Write("Number 2: "); Num2 = int.Parse(Console.ReadLine()); //input for first number is valid Num2OK = true; } catch (Exception E) { //display error information Console.WriteLine("Error: " + E.Message); } } //display result Console.WriteLine("Total: " + AddNumbers(Num1, Num2)); //finish Console.WriteLine("Press any key to continue..."); Console.Read(); } //function for adding two numbers static int AddNumbers(int Number1, int Number2) { //add numbers and return result return (Number1 + Number2); } } }Cheers, Shuaib Edited January 22, 2003 by VisualDeveloper Quote Windows XP Professional, Visual Studio .NET Enterprise Architect, Dell Dimension 8100, 15" TFT Monitor, Intel Pentium 4 CPU 1700MHz, 40GB Hard Disk, nVidia GeForce 3, NEC CD-RW 16x, Samsung DVD 16x.
Moderators Robby Posted January 22, 2003 Moderators Posted January 22, 2003 Placing Tr/Catch in a loop is not a great idea if there is no way out from the Catch. Quote Visit...Bassic Software
White Rose Posted February 13, 2003 Posted February 13, 2003 In C#, how to determine if the string is a number or not? Quote
*Experts* Nerseus Posted February 13, 2003 *Experts* Posted February 13, 2003 Try something like this: private bool IsInt(string test) { try { int dummy = Int32.Parse(test); return true; } catch { return false; } } There is no built in "IsNumeric" function in .NET. VB.NET supports the old IsNumeric function (I think) but only for backwards compatability. I wouldn't recommend using it. Also, using the Int32.Parse() method, you can specify extra options like if you want to allow commas as part of a valid string or not. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* Derek Stone Posted February 14, 2003 *Gurus* Posted February 14, 2003 I'd use the following instead, to avoid error handling at all costs: private bool IsInt(string sTest) { if (System.Text.RegularExpressions.Regex.IsMatch(sTest, @"\d")) { return true; } } Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.