ERROR: Cannot Implicitly Convert Type 'string' to 'int'

Joined
Jan 21, 2003
Messages
20
Location
London, UK
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.

C#:
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
 
Last edited by a moderator:
Ive been looking into the so-called "Try...Catch" statement this afternoon!

Is there any way my program below could be improved or optimized?
C#:
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
 
Last edited by a moderator:
Try something like this:
C#:
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 wouldnt 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
 
Id use the following instead, to avoid error handling at all costs:

C#:
private bool IsInt(string sTest) {
    if (System.Text.RegularExpressions.Regex.IsMatch(sTest, @"\d")) {
        return true;
    }
}
 
Back
Top