0

I have a method (AlphaCalcResult) that should return multiple values. Each forloop has some logic which assigns value to a List field. I would then like to return the result object with the two values generated from two loops described below. New to C# development and having issues with the syntax.

How can I have return statement do that in my code below?

public AlphaCalcResult CalculateAlpha(AlphaCalcParam param)
{
    AlphaCalcResult result = new AlphaCalcResult
    {
        portfolios = new List<Portfolio>()
    };

    // Lists Portfolios & Settings
    var portfolioListItem = param.portfolios.portfolioHoldings;
    var scoreListItem = param.settings.grossAlphas;

    // 1. CALCULATE FUND ALPHA
    for (var i = 0; i<portfolioListItem.Count; i++)
    {
        portfolioListItem[i].fundRating = i;

        for (var j = 0; j<scoreListItem.Count; j++)
        {
            scoreListItem[j].fundRating = j;

            if(i == j)
            {
               portfolioListItem[i].fundAlpha = scoreListItem[j].grossAlpha - portfolioListItem[i].fundExpenseRatio;
            }
        }
    }

    //2. CALCULATE PORTFOLIO ALPHA
    var portfolioAlphaResult = param.portfolios.portfolioAlpha;
    for (var i =0; i<portfolioListItem.Count; i++)
    {
        portfolioAlphaResult = portfolioListItem[i].fundWeight * portfolioListItem[i].fundAlpha;
    };

    // populate result
    return result;
    }
7
  • 3
    You are never actually populating result, you are creating an empty list, doing a load of work that never adds to that list, and returning the empty list Commented Feb 22, 2017 at 15:07
  • can you direct me into the right direction please? Commented Feb 22, 2017 at 15:08
  • 2
    I thought i did.... You are calculating portfolioAlphaResult, but never actually adding it to result.portfolios. Commented Feb 22, 2017 at 15:09
  • can you tell which 2 lists you want to return? or you want to add portfolioAlphaResult in result? Commented Feb 22, 2017 at 15:17
  • 1
    portfolioIdentifier will be always set to current? Commented Feb 22, 2017 at 15:48

3 Answers 3

1

first change public IEnumerable<Portfolio> portfolios { get; set; } to

public List<Portfolio> portfolios { get; set; } 

because IEnumerable is used for reading the list and in your code add

 //2. CALCULATE PORTFOLIO ALPHA
         for (var i =0; i<portfolioListItem.Count; i++)
            {

        List<PortfolioHolding> list = new List<PortfolioHolding>();
        list=portfolioListItem;
        Portfolio port = new Portfolio();
        port.portfolioIdentifier = "current";
        port.portfolioAlpha = portfolioListItem[i].fundWeight * portfolioListItem[i].fundAlpha;
        port.portfolioHoldings = list;
        result.portfolios.Add(port);

        };
Sign up to request clarification or add additional context in comments.

1 Comment

Is this another for loop or an existing one?
1

I'm not sure if this is really what you'd do in this instance, but you can return multiple values using .NET 4.0+'s Tuple type.

For Example:

public Tuple<string, int> GetValues()
{
     return Tuple.Create("first",2);
}

// usage:
var result = GetValues();
var theString = result.Item1;
var theInt = result.Item2;

Comments

1

Change this part :

//2. CALCULATE PORTFOLIO ALPHA
var portfolioAlphaResult = param.portfolios.portfolioAlpha;
for (var i =0; i<portfolioListItem.Count; i++)
{
    portfolioAlphaResult = portfolioListItem[i].fundWeight * portfolioListItem[i].fundAlpha;
    (result.portfolios as List<Portfolio>).Add(new Portfolio() { portfolioAlpha = portfolioAlphaResult });

};

1 Comment

Tried but getting an error IEnumerable<Portfolio> does not contain definition for Add and no extension method Add accepting a first argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.