简体   繁体   中英

Modifying method to accept different number and type of parameters

I'm working on this project with some other colleges. It's build on Windows MDI forms and we have a custom method for loading the forms due to the specific need of the customers.

At the begining the method had to take only two parameters :

protected void LoadForm<T>(ToolStripButton formButton, string buttonText) where T : Base Form

and because even that was inherited code we inherit some forms that didn't need to pass any parameters so we had this method:

protected void LoadForm<T>()
{
    LoadForm<T>(null, null);
}

Then my college needed to pass another parameter : long? EntityId long? EntityId and again to not break the existing code he changed the above method to :

protected void LoadForm<T>(long? loadEntityId = null) where T : BaseForm
{
    LoadForm<T>(null, null, loadEntityId);
}

And the actual method with the business logic became:

protected void LoadForm<T>(ToolStripButton formButton, string buttonText, long? loadEntityId = null) where T : BaseForm

Now I need to pass fourth parameter myself and again the issue with not breaking the previous code is actual. I thought I got the logic right but it seems that I haven't. I need to pass a boolean parameter bool IsClosingForm so I made the method witht he business logic like this:

protected void LoadForm<T>(ToolStripButton formButton, string buttonText, long? loadEntityId = null, bool IsClosingForm= true) where T : BaseForm

And add new method to handle situations when the bool parameter need to be passed:

protected void LoadForm<T>(bool IsClosingForm= true) where T : BaseForm
{
    LoadForm<T>(null, null, null, IsClosingForm);
}

And also modify my colleges method to call a four parameter method:

protected void LoadForm<T>(long? EntityId = null) where T : BaseForm
{
    LoadForm<T>(null, null, EntityId, true);
}

But it's not working like that and the error I get is:

The call is ambiguous between the protected void LoadForm<T>(long? EntityId = null) and protected void LoadForm<T>(bool IsClosingForm= true) method

It's obvious I'm not getting the logic here so I have two questions - how to modify the code to make it work and if possible I would like someone to point what cause my mistake so I can get the logic behind this. First it seemed very easy task, but now I think I'm missing something that I should not leave not understood.

This is the ambiguous call:

LoadForm<T>()

The call can be interpreted as one of the following two methods:

  • LoadForm<T>(long? EntityId = null)
  • or as LoadForm<T>(bool IsClosingForm= true)

Since you have specified a default value for the parameters, the compiler cannot distinguish between the two overloads when the argument is not supplied.

Maybe you can remove the default values and create another overload that does not expect any argument. This new overload would be used when LoadForm is invoked without parameters

Is it possible that you are passing in 0 or 1 in and its an ambiguous as to it being a bool or long? Possibly cast it to a long in the method call to ensure getting registered as long and not bool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM