0

I recently came across an issue where i cannot have optional parameters in function in a WinRT project.

Are there any alternatives to this ? I even tried the [optional] keyword. Does not work.

golldy
  • 1,279
  • 1
  • 15
  • 31
  • You should be able to just assign a default value to the parameter, see this post: http://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c – dub stylee Aug 31 '15 at 23:24
  • "i cannot have optional parameters": what do you mean by that? Do you get a compile error? What's the error? – Thomas Levesque Aug 31 '15 at 23:25
  • In order to use the `[Optional]` keyword, you need to be sure to add `using System.Runtime.InteropServices;` – dub stylee Aug 31 '15 at 23:38
  • Thomas, thats the error that i get. Optional Parameters are not allowed in WINRT functions. – golldy Sep 01 '15 at 06:32
  • Dub Style, i understand how referencing works..This is not a reference issue. – golldy Sep 01 '15 at 06:34

3 Answers3

2

In Windows runtime component project public functions cannot have optional parameters only private functions can have them.

But if you convert that project to a class library, even for public functions you can have optional parameters.

golldy
  • 1,279
  • 1
  • 15
  • 31
  • Ah, that may be what the issue is with my answer. In my testing, I was using `private` functions in both scenarios. I hadn't tried in a `public` function. My bad. – dub stylee Sep 01 '15 at 23:32
1

An other possible approach : use an overrided signature.

public void TheFunction (string param1, string param2)
{
  [...] //processing stuff
}

public void TheFunction (string param1)
{
   return TheFunction(param1, String.Empty);
}
Guillaume Raymond
  • 1,726
  • 1
  • 20
  • 33
-1

The following will not compile:

void PrintStuffOptional(string stuff, [Optional] int num)
{
    Console.WriteLine(stuff + ": " + num.ToString());
}

Resulting in:

The type or namespace name 'Optional' could not be found (are you missing a using directive or an assembly reference?)

and/or:

The type or namespace name 'OptionalAttribute' could not be found (are you missing a using directive or an assembly reference?)

Adding using System.Runtime.InteropServices; at the top of your file should correct those issues. However, as of C# 4.0, you can declare optional parameters like so:

void PrintStuff(string stuff, int num = 0)
{
    Console.WriteLine(stuff + ": " + num.ToString());
}

If the calling method does not provide a value for the num parameter, it will use the default value of 0. Therefore, the void PrintStuff() will work both ways:

PrintStuff("a string to print");
PrintStuff("a string to print", 37239);
dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • This specific to WinRT function. – golldy Sep 01 '15 at 06:41
  • Did you even try my answer? I just tried BOTH methods in a new WinRT application, and BOTH of them work just fine for using optional parameters. If you are still having problems, provide more information and maybe some code. Otherwise we can only assume that the problem lies between your keyboard and your chair. – dub stylee Sep 01 '15 at 13:48
  • Try your code on a Windows phone 8.1 WinRT project and let me know if that works. I am really not that dumb a programmer not to add a using statement on the page to refer something. Come on, i think stackoverflow has better developers. Just trying to seek help. – golldy Sep 01 '15 at 15:47
  • I did try my code on a Universal WinRT project. Both the Windows and the Windows Phone portion. Both variations of my answer worked for me. If you could add some code to your question, you are more likely to get some help. – dub stylee Sep 01 '15 at 16:15
  • Try your code on a WindowsRuntimeComponent and class library One will work and one wont – golldy Sep 01 '15 at 23:09
  • Since you still haven't posted any of your code that is giving you problems, I am going to assume you don't want help. You didn't initially mention anything about WindowsRuntimeComponent or class library. I was able to use optional parameters, with both solutions I posted in my answer, in a universal WinRT project. – dub stylee Sep 01 '15 at 23:30