Re: Anything wrong with the way I use "break"s in my loops?
- From: "Norman Yuan" <FakeName@xxxxxxxxxxxxx>
- Date: Wed, 28 Jan 2009 16:58:42 -0700
There isn't anything logically wrong the way you use "break" twice to get out of the 2 loops, but a few lines of tedious code. You can make the code a bit more concise by immediately returning the found value in the inner loop:
foreach (...)
{
....
foreach (BMarkUnit bu in bp.AggregateLists)
{
if ( bu.ListName == list )
{
return bu.OriginalWeight;
}
}
}
As for execution efficientcy, I do not think it makes noticeable difference.
"Curious" <fir5tsight@xxxxxxxxx> wrote in message news:11e6757d-66b9-4491-b5ca-474b1f7bd59a@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have a simple method that does a search. Details below:
"mBenchmarkPriceList" is an ArrayList with each element being of
"BenchmarkPrice" type. There are three members in "BenchmarkPrice":
Ticker, Side, and AggregateLists.
The member, "AggregateLists", however, is an ArrayList with each
element being a "BMarkUnit" type. "BMarkUnit" contains two members,
ListName and OriginalWeight.
The method, "GetOriginalWeight", searches through the entire
"mBenchmarkPriceList". If a match of Ticker, Side, and also ListName
is found, it will return "OriginalWeight".
In order to be efficient, once a match is found, I "break" both loops
and return the value for "OriginalWeight". However, it seems that an
incorrect value is returned most of the time. Is there anything wrong
with the way I use either "break"?
Thanks!
double GetOriginalWeight ( string list, string ticker, string
side )
{
double origWeight = 0.0;
bool found = false;
foreach (BenchmarkPrice bp in mBenchmarkPriceList)
{
if ( bp.Ticker == ticker &&
bp.Side == side || ( side == "Buy" && bp.Side ==
"Cover" ) || ( side == "Sell" && bp.Side == "Short" ))
{
foreach (BMarkUnit bu in bp.AggregateLists)
{
if ( bu.ListName == list )
{
origWeight = bu.OriginalWeight;
found = true;
break;
}
}
}
if (found)
{
break;
}
}
return origWeight;
}
.
- Follow-Ups:
- References:
- Anything wrong with the way I use "break"s in my loops?
- From: Curious
- Anything wrong with the way I use "break"s in my loops?
- Prev by Date: Anything wrong with the way I use "break"s in my loops?
- Next by Date: Re: Anything wrong with the way I use "break"s in my loops?
- Previous by thread: Anything wrong with the way I use "break"s in my loops?
- Next by thread: Re: Anything wrong with the way I use "break"s in my loops?
- Index(es):
Relevant Pages
|