none
Removing unused optional parameter causes error in calling code RRS feed

  • Question

  • I have a class library where I have a class with one required parameter and one optional parameter.  The optional parameter is no longer used and all references to it were removed from calling code months ago.  I just removed the parameter itself from the method, recompiled the class into a DLL, and updated the DLL in the website that uses it.  When I run the website locally (haven't gotten past local testing), and the method is called, I get an error that says "Method not found: 'System.String MyNamespace.MyClass.MyMethod(Byte[], Int32)'."  The "Int32" this message is referring to is the optional parameter that I removed.  The website compiles fine because it doesn't need a value for the Int32, but even bringing this reference up in the Object Browser still shows that the Integer is there, when it really isn't!  Is there some settings in my compiler that I need to fix in order for the website to recognize that the optional parameter is gone and it should be able to find a method with signature  'System.String MyNamespace.MyClass.MyMethod(Byte[])' ?  I can not figure out why this is just not working properly.

    TIA

    -VG


    VG_mnet

    Friday, August 9, 2013 2:18 PM

Answers

  • "even though nothing has changed (because the optional parameter was not used)."

    Nope, those DLL changed too when you removed the parameter itself. If you have a method like

    void foo(int a, int b = 42) {}

    and you call it like

    foo(2);

    then the compiler replaces foo(2) with foo(2, 42).

    Now, if you remove the b parameter all the DLLs that use that method will change because now the compiler generates foo(2) instead of foo(2, 42).

    In summary, after such a change you have to make sure that all assemblies have been recompiled and deployed. To be sure rebuild the solution and copy all the necessary dlls to that web site, not only the one which contains the method.

    • Proposed as answer by Damon Bu - MSFT Tuesday, August 13, 2013 5:08 AM
    • Marked as answer by VG_Developer Tuesday, August 13, 2013 2:28 PM
    Friday, August 9, 2013 2:37 PM
    Moderator

All replies

  • " I just removed the parameter itself from the method, recompiled the class into a DLL, and updated the DLL in the website that uses it"

    But the question is, did you also update all other DLLs that were calling that method?

    Friday, August 9, 2013 2:24 PM
    Moderator
  • Good question.  Yes, all other DLLs that were also calling that method have been recompiled as well even though nothing has changed (because the optional parameter was not used).  I recompiled them just in case they need to be recompiled with the new reference. 

    Thanks


    VG_mnet

    Friday, August 9, 2013 2:26 PM
  • "even though nothing has changed (because the optional parameter was not used)."

    Nope, those DLL changed too when you removed the parameter itself. If you have a method like

    void foo(int a, int b = 42) {}

    and you call it like

    foo(2);

    then the compiler replaces foo(2) with foo(2, 42).

    Now, if you remove the b parameter all the DLLs that use that method will change because now the compiler generates foo(2) instead of foo(2, 42).

    In summary, after such a change you have to make sure that all assemblies have been recompiled and deployed. To be sure rebuild the solution and copy all the necessary dlls to that web site, not only the one which contains the method.

    • Proposed as answer by Damon Bu - MSFT Tuesday, August 13, 2013 5:08 AM
    • Marked as answer by VG_Developer Tuesday, August 13, 2013 2:28 PM
    Friday, August 9, 2013 2:37 PM
    Moderator
  • OK, thanks for the information.  I did not know that.  I did already recompile the other libraries, but to be safe, I will do it again in case I missed one, or forgot to "get latest" before recompiling one.


    VG_mnet

    Friday, August 9, 2013 2:42 PM