Optional parameters in User code method?

Best practices, code snippets for common functionality, examples, and guidelines.
mrt
Posts: 88
Joined: Mon Mar 16, 2020 11:31 am

Optional parameters in User code method?

Post by mrt » Wed Mar 10, 2021 9:45 am

Dear folks,

is it somehow possible to use optional parameters in UserCodeMethod?

I use the same method, sometimes it is necessary to pass a DivTag element and sometimes I don't need it, so I would want to do something like:

Code: Select all

[UserCodeMethod]
public static void MyMethod(RepoItemInfo divtaginfo = null)
{ ... }
or

Code: Select all

[UserCodeMethod]
public static void MyMethod([RepoItemInfo divtaginfo = null])
{ ... }
But compilation always fails on not finding a () parameterless user code method.

Is this not possible or what am I doing wrong?

thank you, BR mrt

Wispy
Posts: 1
Joined: Wed Mar 31, 2021 10:48 am

Re: Optional parameters in User code method?

Post by Wispy » Wed Mar 31, 2021 10:57 am

I am not sure if this is possible as I had the same issue.

The way I handled this was create an overload of my method so that I could call with or without my optional parameter. The overload without my parameter just calls the one with the parameter passing in the default value I want. For your example it would look somthing like:

Code: Select all

[UserCodeMethod]
public static void MyMethod()
{
	MyMethod(null);
}

[UserCodeMethod]
public static void MyMethod([RepoItemInfo divtaginfo])
{
	//Do Stuff
}
I know its not exatly what you are after but it was a suitable solution in my instance so might help you

tyshawnthiel
Posts: 1
Joined: Tue Apr 06, 2021 7:21 am

Re: Optional parameters in User code method?

Post by tyshawnthiel » Tue Apr 06, 2021 7:38 am

Thanks Wispy bubble shooter
it helps me a lot

mrt
Posts: 88
Joined: Mon Mar 16, 2020 11:31 am

Re: Optional parameters in User code method?

Post by mrt » Thu Apr 08, 2021 7:31 am

Thanks, good workaround!