473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 PublishNegativeAlpha ( IGridEntry entry)
{
try
{
foreach (NegativeAlphaForEntry negA in mNegativeAList)
{

if (negA.List == entry.List.Name &&
negA.Ticker == entry.Ticker &&
negA.TargetShares == entry.TargetShares)
{

ESide side = entry.TargetSide;
string strSide = GetSideString(side);

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

CustomObjectManager.Publish(neg,
PublishMode.Regular);
logger.LogMessageBarMessage(ESeverity.Detail, "Published negative
alpha for ticker " + negA.Ticker);

break;
}
}
}
}
catch (Exception e)
{
logger.LogMessageBarMessage(ESeverity.Error,
e.Message);
}
}
Method #2:

void PublishNegativeAlpha ( IGridEntry entry, HtmlLogger
varLogger)
{
try
{
foreach (NegativeAlphaForEntry negA in mNegativeAList)
{

if (negA.List == entry.List.Name &&
negA.Ticker == entry.Ticker &&
negA.TargetShares == entry.TargetShares)
{

ESide side = entry.TargetSide;
string strSide = GetSideString(side);

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

CustomObjectManager.Publish(neg,
PublishMode.Regular);
varLogger.LogMessageBarMessage(ESeverity.Detail, "Published negative
alpha for ticker " + negA.Ticker);

break;
}
}
}
}
catch (Exception e)
{
varLogger.LogMessageBarMessage(ESeverity.Error,
e.Message);
}
}
Aug 3 '08 #1
2 1288
Curious <fi********@yahoo.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 PublishNegativeAlpha(entry)
to:

void PublishNegativeAlpha (IGridEntry entry)
{
PublishNegativeAlpha(entry, logger);
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 3 '08 #2
Hi, Jon,

That's a great idea! Thanks.
Aug 3 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MLH | last post by:
The following function declaration confuses me... Public Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean I tried pasting it and its code into an...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
10
by: John Morgan | last post by:
Does anyone know what parameter should be used instead of Date = 0 for the optional parameter in the following function? Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date...
10
by: deko | last post by:
In VB, I could do this: MyFuncrion(this As String, that As Integer, Optional otherThing As Boolean) do stuff here End Function In C#, I can use "out" to return multiple values from a method,...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.