Microsoft.VisualBasic; IRR function

Hi I’m coding in c# and I’m usiing the IIR function of Microsoft.VisualBasic.

I need values for 6 years that’s 72months

if (i <= 72)
{
values[i] += C158_Calc;
}

    B162_Calc = Financial.IRR(ref values, 0.1) * 12;
    row9["Month1"] = String.Format("{0:#,###,###,###.##}", B162_Calc);

this gives me this error:

Arguments are not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Arguments are not valid.

but if I say up to 10months it works, more than that not.

Any help please.

double values = new double[72];

That line makes the size of the array 72. But you must remember they are 0 based. So the max index of the values will be 71.

So if i = 72, that will not work. Also, what about all the other values? Should something like that not be added in a while loop or something? As the reference to the values with no actual values will probably not work

O sorry this if statement is inside a forloop.


public DataSet GetTable()
    {
        double H30_Out = -63261257;
        double H91_Out = 5640000;
        double H94_Out = -846000;

        C146_Calc = H91_Out / 12;
        C148_Calc = H94_Out / 12;

        double G158_In = 8;
        double G164_In = 7;

        double F176_In = 5;
        double G176_In = 2.5;

        C150_Calc = C146_Calc + C148_Calc;
        C151_Calc = -(C150_Calc * F176_In) / 100;

        double G169_In = 11;
        double G170_In = 14;

        double B162_Calc = 0.0;

        double[] values = new double[73];

        DataTable dt = new DataTable("Details");
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);

        for (int i = 0; i <= 240; i++)
        {
            dt.Columns.Add("Month" + i, typeof(string));
        }

        dt.Columns.Add("End", typeof(string));

        DataRow dr = dt.NewRow();
        dr["Month1"] = String.Format("{0:#,###,###,###.##}", H30_Out);
        dt.Rows.Add(dr);

        DataRow row1 = dt.NewRow();
        DataRow row2 = dt.NewRow();
        DataRow row3 = dt.NewRow();
        DataRow row4 = dt.NewRow();
        DataRow row5 = dt.NewRow();
        DataRow row6 = dt.NewRow();
        DataRow row7 = dt.NewRow();
        DataRow row8 = dt.NewRow();
        DataRow row9 = dt.NewRow();

        dt.Rows.Add(row1);
        dt.Rows.Add(row2);
        dt.Rows.Add(row3);
        dt.Rows.Add(row4);
        dt.Rows.Add(row5);
        dt.Rows.Add(row6);
        dt.Rows.Add(row7);
        dt.Rows.Add(row8);
        dt.Rows.Add(row9);

        for (int i = 0; i <= 240; i++)
        {
            double num = 0.0;
            double num2 = 0.0;

            if (i == 13 || i == 25 || i == 37 || i == 49 || i == 61 || i == 73 ||
                i == 85 || i == 98 || i == 109 || i == 121 || i == 133 || i == 145 ||
                i == 157 || i == 169 || i == 181 || i == 193 || i == 205 || i == 217 ||
                i == 229)
            {
                num = (C146_Calc * G158_In) / 100;
                num2 = (C148_Calc * G164_In) / 100;
            }
            C146_Calc += num;
            C148_Calc += num2;
            C150_Calc = C146_Calc + C148_Calc;

            if (i == 13 || i == 25 || i == 37 || i == 49 || i == 61 || i == 73 ||
                i == 85 || i == 98 || i == 109 || i == 121 || i == 133 || i == 145 ||
                i == 157 || i == 169 || i == 181 || i == 193 || i == 205 || i == 217 ||
                i == 229)
            {
                C151_Calc = -(C150_Calc * G176_In) / 100;
            }

            double C152_Calc = C150_Calc + C151_Calc;

            double BW154_Calc = 0.0;
            if (i == 72)
                BW154_Calc = (C152_Calc * 12 / G169_In) * 100;
            if (i == 240)
                BW154_Calc = (C152_Calc * 12 / G170_In) * 100;

            num3 += BW154_Calc;

            
            double C158_Calc = 0.0;
                // calculate 6 year IRR
            //if (i == 1)
            //    C158_Calc = H30_Out + C152_Calc;
            if (i == 0)
                C158_Calc = H30_Out + C152_Calc;
            if (i >= 1 && i <= 72)  // 2 not 1
                C158_Calc = C152_Calc;
            if (i == 73)
                C158_Calc = C152_Calc + num3;

            if (i <= 72)
            {
                values[i] += C158_Calc;
            }

                // calculate 20 year IRR
            double C160_Calc = 0.0;
            if (i == 1)
                C160_Calc = H30_Out + C152_Calc;
            else
                C160_Calc = C152_Calc;

            row1["Month" + i] = String.Format("{0:#,###,###,###}", C146_Calc);
            row2["Month" + i] = String.Format("{0:#,###,###,###}", C148_Calc);
            row3["Month" + i] = String.Format("{0:#,###,###,###}", C150_Calc);
            row4["Month" + i] = String.Format("{0:#,###,###,###}", C151_Calc);
            row5["Month" + i] = String.Format("{0:#,###,###,###}", C152_Calc);
            row6["Month" + i] = String.Format("{0:#,###,###,###}", BW154_Calc);
            row7["Month" + i] = String.Format("{0:#,###,###,###}", C158_Calc);
            row8["Month" + i] = String.Format("{0:#,###,###,###}", C160_Calc);
        }
            // total 6 year IRR
        B162_Calc = Financial.IRR(ref values, 0.1) * 12;
        row9["Month1"] = String.Format("{0:#,###,###,###.##}", B162_Calc);

        return ds;
    }

Hi, thanks for responding, when I call this line
B162_Calc = Financial.IRR(ref values, 0.1) * 12;
I get the issue.

This is how values are declared.
double values = new double[72];

if I say:
if (i <= 10)
{
values[i] += C158_Calc;
}

then it works, or any value smaller than 10, else I get the error:
Arguments are not valid.

What line is causing the error? And what container type is values? ie. How are you declaring it? Sorry, I am not familiar with IRR, but maybe I can try and help spot something