PRB: "Invalid Argument" Error Message When You Compile a Visual C# .NET Console Application

This article was previously published under Q311156
When you compile a Visual C# .NET console application, you may receive the following error message:
error CS1502: The best overloaded method match for 'declaration' has some invalid arguments
NOTE: The CS1502 error message is always accompanied by the CS1503 error message:
error CS1503: Argument 'arg': cannot convert from 'type1' to 'type2'
The argument types that are passed to the function do not match the parameter types of that function. If the called function is overloaded, none of the overloaded versions has a signature that matches the argument types that are passed.
To resolve this problem, do any of the following:
  • Double-check the types of the arguments that are passed to make sure that they match the arguments of the function that is called. Correct any coding errors that may lead to this problem.
  • If appropriate, convert any of the mismatched parameters by using the System.Convert class.
  • If appropriate, cast any of the mismatched parameters to match the type that the function expects.
  • If appropriate, define another overloaded version of the function to match the parameter types that are sent.
This behavior is by design.
In the following sample Visual C# .NET console application, the Test function has two overloaded versions. The first version takes only an int type. The second version expects an int type as its first argument and a string type as its second argument. However, in the Main function, the first argument that is passed (nNum) is a string type. Neither of the two overloaded Test functions takes a string type as the first parameter.
using System;namespace x{   public class CTest	   {      public static void Test(int n1) {}      public static void Test(int n1, string str){}      public static void Main()      {         string nNum = "1000";         CTest.Test(nNum, "string2"); //CS1502      }   }}				
One way to resolve the error is to declare nNum as an integer, as follows:
int nNum = 1000;				
Another solution is to convert the string into an integer before you call the Test function. The following code resolves the compiler error and produces the desired result:
string nNum = "1000";int nNumConverted = System.Convert.ToInt32(nNum, 10);CTest.Test(nNumConverted, "string2");//CS1502				

Article ID: 311156 - Last Review: 07/29/2002 02:23:00 - Revision: 1.0

Microsoft Visual C# .NET 2002 Standard Edition

  • kbprb kbprod2web KB311156