locked
Optional parameters for WinRT component

    Question

  • Hello,

    I have this method signature:

    void createThumbnail(Platform::String^ thumbnailPath, int size);

    and would like the size param to be optional with a default value. Is that possible?

    Friday, March 02, 2012 2:44 PM

Answers

  • You can use the defualt argument for a private function in winrt component. But if the function is public, it means that this function with default arguments can be called across the component, which is not allowed. 

    Best Regards,
    Han XIa

    Monday, March 05, 2012 2:26 PM
    Moderator

All replies

  • Try this:

    void createThumbnail(Platform::String^ thumbnailPath, int size = 1);

    Read the 'Note these points when using default arguments' section in this document: Default Arguments for specific rules.

    Thanks,

    -David

    Friday, March 02, 2012 11:42 PM
    Moderator
  • Thanks David, I tried this (standard c++) and I got:

    Error 1 error C3222: 'size' : cannot declare default arguments for member functions of a WinRT type or generic functions

    :/

    Friday, March 02, 2012 11:54 PM
  • Hello,

    I cannot reproduce your issue on my site. Would you please show us the whole codes to reproduce this issue?

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 05, 2012 9:02 AM
  • You can use the defualt argument for a private function in winrt component. But if the function is public, it means that this function with default arguments can be called across the component, which is not allowed. 

    Best Regards,
    Han XIa

    Monday, March 05, 2012 2:26 PM
    Moderator
  • @phil_ke I see what you were trying to do now. As Han Xi mentioned, default or optional parameters are not supported on the WinRT surface. Using them internally works fine, but not on the surface of your WinRT component.

    Thanks,

    -David 

    Monday, March 05, 2012 3:01 PM
    Moderator
  • Pitty. In my own Active/JavaScript-COM Wrapper I handled empty arguments, since that is part of COM already. The compiler that generates the "winmd" file could certainly substitute empty VARIANT arguments coming from JavaScript with the defined default parameter as well. But I guess we have to live with that now. No big deal.
    Monday, March 05, 2012 5:09 PM