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

Article translations Article translations
Article ID: 311156 - View products that this article applies to.
This article was previously published under Q311156
Expand all | Collapse all

SYMPTOMS

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'

CAUSE

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.

RESOLUTION

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.

STATUS

This behavior is by design.

MORE INFORMATION

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
				

Properties

Article ID: 311156 - Last Review: July 29, 2002 - Revision: 1.0
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbprb kbprod2web KB311156

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com