0

I've initialized a 2 dimensional array and starting from index 5 onwards contain null value. I'm trying to add the array values to my Chart Control. When I try displaying the values to Console.WriteLine, there's no exception occurred however when I add to my chart series, the exception is "Index was outside the bounds of the array." Anyone can help me? is it because chart control has limited bars?

try
{
    //females age range
    string[,] gAge = new string[10, 2];
    gAge[0, 0] = "F.13-17";
    gAge[0, 1] = yf.f1317.ToString();
    gAge[1, 0] = "F.18-24";
    gAge[1, 1] = yf.f1824.ToString();
    gAge[2, 0] = "M.25-34";
    gAge[2, 1] = yf.m2534.ToString();
    gAge[3, 0] = "M.13-17";
    gAge[3, 1] = yf.m1317.ToString();
    gAge[4, 0] = "M.18-24";
    gAge[4, 1] = yf.m1824.ToString();

    for (int i = 0; i < gAge.Length; i++)
    {

        if (gAge[i, 0] == null)
        {
            break;
        }
        else
        {
            Console.WriteLine(gAge[i, 0].ToString());
            string[] seriesArray = { gAge[i, 0].ToString() };
            Series series = this.chart1.Series.Add(seriesArray[i]);
        }

    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
2
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Jan 28, 2013 at 4:56
  • @JohnSaunders oops sorry. thanks for pointing that out =D Commented Jan 28, 2013 at 6:48

2 Answers 2

1

The problem is that you are getting the length of your entire array, in this casegAge.Length is equal to 20 which is the total number of indices in your Array. It is exceeding the bounds of your first array dimension. Try using gAge.GetLength(0) instead.

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

3 Comments

hello Mark. thank you for your help. Ive changed my for loop to for (int i = 0; i < gAge.GetLength(0); i++) but it seems that the program throws an exception when it executes this line > "Series series = this.chart1.Series.Add(seriesArray[i]);"
hey, I think I got it! Its my own silly mistake. I got the error is because I didnt do this: string[] seriesArray = { gAge[i,0] }; and I didnt do another for loop in order to loop through my seriesArray to add the values into my series! Thank you so much Mark for the pointing my first error out! ^_^
@SRA You are Welcome, glad to be of help.
0
 try
        {
            chart1.Series.Clear();
            chart1.Titles.Clear();
            chart1.Visible = true;

            //CODES HERE TO GET THE SELECTED PAGE FROM DROP DOWN LIST

            var fb = new FacebookClient(myToken.Default.token);
            var query = string.Format("SELECT metric, value FROM insights WHERE object_id=132414626916208 AND metric='page_fans_gender_age' AND period = period('lifetime') AND end_time = end_time_date('2013-01-18')");

            dynamic parameters = new ExpandoObject();
            parameters.q = query;
            var data = fb.Get<genderAgeDataII>("fql", parameters);

            genderAge yf = (genderAge)data.data[0].value;

            //females age range
            string[,] gAge = new string[10, 2];
            gAge[0, 0] = "F.13-17";
            gAge[0, 1] = yf.f1317.ToString();
            gAge[1, 0] = "F.18-24";
            gAge[1, 1] = yf.f1824.ToString();
            gAge[2, 0] = "M.25-34";
            gAge[2, 1] = yf.m2534.ToString();
            gAge[3, 0] = "M.13-17";
            gAge[3, 1] = yf.m1317.ToString();
            gAge[4, 0] = "M.18-24";
            gAge[4, 1] = yf.m1824.ToString();

            for (int i = 0; i < gAge.GetLength(0); i++)
            {

                if (gAge[i, 0] == null)
                {
                    break;
                }
                else
                {

                   string[] seriesArray = { gAge[i,0] };

                   // Add series.
                   for (int w = 0; w < seriesArray.Length; w++)
                   {
                       // Add series.
                       Series series = this.chart1.Series.Add(seriesArray[w]);

                       int[] pointsArray = new int[seriesArray.Length];
                       pointsArray[w] = Convert.ToInt32(gAge[i, 1]);
                       series.Points.Add(pointsArray[w]);
                   }


                   Console.WriteLine(gAge[i, 0].ToString());
                }

            }

Comments

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.