Microsoft KB Archive/311156

= PRB: &quot;Invalid Argument&quot; Error Message When You Compile a Visual C# .NET Console Application =

Article ID: 311156

Article Last Modified on 7/29/2002

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q311156



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 = &quot;1000&quot;; CTest.Test(nNum, &quot;string2&quot;); //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 = &quot;1000&quot;; int nNumConverted = System.Convert.ToInt32(nNum, 10); CTest.Test(nNumConverted, &quot;string2&quot;);//CS1502

Keywords: kbprb kbprod2web KB311156

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.