Optional parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Curious

    Optional parameter

    I have two methods that are almost identical, except for the
    "HtmlLogger varLogger" parameter passed to #2.
    In #1, it uses a default HtmlLogger "logger" while in #2, it uses
    "varLogger" parameter.

    Other than that, the two methods are identical. I would like to
    combine the two methods into a single method, but I wish to know:

    - How to identify the optional parameter in the method; and
    - When there is no optional parameter passed to the method, how to set
    the default as "logger".

    Method #1:

    void PublishNegative Alpha ( IGridEntry entry)
    {
    try
    {
    foreach (NegativeAlphaF orEntry negA in mNegativeAList)
    {

    if (negA.List == entry.List.Name &&
    negA.Ticker == entry.Ticker &&
    negA.TargetShar es == entry.TargetSha res)
    {

    ESide side = entry.TargetSid e;
    string strSide = GetSideString(s ide);

    if (negA.Side == strSide)
    {
    NegativeAlpha neg = new NegativeAlpha() ;
    neg.ObjectId = entry.ObjectId;
    neg.NegativeSta tus = "Negative Alpha";

    CustomObjectMan ager.Publish(ne g,
    PublishMode.Reg ular);


    logger.LogMessa geBarMessage(ES everity.Detail, "Published negative
    alpha for ticker " + negA.Ticker);

    break;
    }
    }
    }
    }
    catch (Exception e)
    {
    logger.LogMessa geBarMessage(ES everity.Error,
    e.Message);
    }
    }


    Method #2:

    void PublishNegative Alpha ( IGridEntry entry, HtmlLogger
    varLogger)
    {
    try
    {
    foreach (NegativeAlphaF orEntry negA in mNegativeAList)
    {

    if (negA.List == entry.List.Name &&
    negA.Ticker == entry.Ticker &&
    negA.TargetShar es == entry.TargetSha res)
    {

    ESide side = entry.TargetSid e;
    string strSide = GetSideString(s ide);

    if (negA.Side == strSide)
    {
    NegativeAlpha neg = new NegativeAlpha() ;
    neg.ObjectId = entry.ObjectId;
    neg.NegativeSta tus = "Negative Alpha";

    CustomObjectMan ager.Publish(ne g,
    PublishMode.Reg ular);


    varLogger.LogMe ssageBarMessage (ESeverity.Deta il, "Published negative
    alpha for ticker " + negA.Ticker);

    break;
    }
    }
    }
    }
    catch (Exception e)
    {
    varLogger.LogMe ssageBarMessage (ESeverity.Erro r,
    e.Message);
    }
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Optional parameter

    Curious <fir5tsight@yah oo.comwrote:
    I have two methods that are almost identical, except for the
    "HtmlLogger varLogger" parameter passed to #2.
    In #1, it uses a default HtmlLogger "logger" while in #2, it uses
    "varLogger" parameter.
    >
    Other than that, the two methods are identical. I would like to
    combine the two methods into a single method, but I wish to know:
    >
    - How to identify the optional parameter in the method; and
    - When there is no optional parameter passed to the method, how to set
    the default as "logger".
    There's no such thing as an optional parameter in C#. The trick is to
    make one overload call the other, supplying an appropriate default (in
    this case "logger"). In other words, change PublishNegative Alpha(entry)
    to:

    void PublishNegative Alpha (IGridEntry entry)
    {
    PublishNegative Alpha(entry, logger);
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Curious

      #3
      Re: Optional parameter

      Hi, Jon,

      That's a great idea! Thanks.

      Comment

      Working...