I am trying to set the value of an overloaded method to 1, but I want the original to remain uninitialised.
public void Method1(string string1, decimal decimal1, int int1)
{
}
public void Method1(string string1, decimal decimal1, int int1 = 1)
{
}
I am getting the following error:
> Error 2 Type 'Application1.Application' already defines a member called 'Method1' with the same parameter types
It does not seem to like the int int1 = 1
. I want int1
to always equal 1 in the second version of the method, but in the first I want it to receive a user entered value, and so is uninitialised for now.
Sorry if this has been asked before, but after an hour of searching I found no results on what I was looking for. If I am missing anything here, please let me know, I will try to give a better description or the further details.
The problem is that you've got two methods with the same signature. The simplest fix to this is just to remove the final parameter from the second overload, and just call the first overload with a hard-coded value for the last argument:
public void Method1(string string1, decimal decimal1, int int1)
{
// Whatever you actually want to do here
}
public void Method1(string string1, decimal decimal1)
{
Method1(string1, decimal1, 1);
}
(If your two methods aren't meant to achieve the same result, you should give them different names to start with.)
If you want to use optional parameters instead, you can do that by just having one method:
public void Method1(string string1, decimal decimal1, int int1 = 1)
{
// Whatever...
}
// Look ma, no second overload
See more on this question at Stackoverflow