How to make optional parameter

Former Member
Former Member

Hi All,

I need to implement an extension method and it suppose to be 2 parameters,

but one is mandatory and another one would be optional parameter.

See the following my logic

public string SubCategoryList(int categoryId, bool isForumThread = false)
{

// Business logic 

}

When I am trying to call the method from Script sand box its expecting two parameters always.

Can you please help me on this.

Thanks in Advance.

Parents Reply
  • Former Member
    0 Former Member in reply to Patrick M.

    Hi Patrick,

    Thank you for the solution, 

    If I have couple of parameters then I can use this approach, but I have lot of parms which are suppose to be optional.

    See the above screen shot, when I look into the document its saying both fields are Required.

    I'm am trying to find a solution how the other widget Apis are implemented.

    ex: core_v2_fourm.List()

    Best Regards,

    Aravind Eriventy.

Children
  • Former Member
    0 Former Member in reply to Former Member

    IDictionary options is how we typically specify groups of optional parameters.  Here is an example of one of the Forum Thread List methods with both required and optional parameters included how they are defined and documented.

            [Documentation("Lists Forum Threads")]
            public PagedList<ForumThread> List(
                [Documentation("Forum Thread Query Type", Options = new string[] { "All", "Moderated", "Unanswered", "Answered", "AnsweredNotVerified", "AnsweredWithNotVerified", "Active", "Unread", "MyThreads", "Authored", "NoResponse" }, Default = "All")]
                string forumThreadQueryType, [
                Documentation(Name = "ForumId", Type = typeof(int), Description = "Forum Id"),
                Documentation(Name = "GroupId", Type = typeof(int), Description = "Group Id"),
                Documentation(Name = "ContentIds", Type = typeof(string), Description = "Comma separated list of ContentIds.  Used to limit to a specific set of forum threads as well as sort order if SoryBy is set to ContentIdsOrder"),
    			Documentation(Name = "SortBy", Type = typeof(string), Options = new string[] { "LastPost", "Date", "Replies", "Views", "Topic", "Votes", "TotalQualityVotes", "QualityScore", "Score:SCORE_ID", "ContentIdsOrder" }, Description = "'LastPost' is sorting by the latest Thread/Reply modification date; 'Date' is sorting by thread created date; 'Votes' sorts by total votes, both quality and interest."),
    			Documentation(Name = "SortOrder", Type = typeof(string), Options = new string[] { "Ascending", "Descending" }, Description = "Default is Descending for lastpost, date, replies, and views. Default is Ascending for topic"),
                Documentation(Name = "PopularOnly", Type = typeof(bool), Description = "Whether to include popular threads only", Default = false),
                Documentation(Name = "IncludeDiscussions", Type = typeof(bool), Description = "Include discussion threads", Default = true),
                Documentation(Name = "IncludeQuestions", Type = typeof(bool), Description = "Include question and answer threads", Default = true),
                Documentation(Name = "IncludeSubGroups", Type = typeof(bool), Description = "Whether to include sub groups", Default = false),
                Documentation(Name = "PageIndex", Type = typeof(int), Description = "Specify the page number of paged results to return. Zero-based index.", Default = 0),
                Documentation(Name = "PageSize", Type = typeof(int), Description = "Specify the number of results to return per page.", Default = 20)
                ]
                IDictionary options)
            {

    Should also note, that while the defined options are optional, the options parameter is required when calling these methods.  If you have no options to pass, you still need to pass an empty Options parameter.

  • Former Member
    0 Former Member in reply to Former Member

    This is what I am looking for.

    Thanks a lot Mathew.