none
Dictionary Index out of range RRS feed

  • Question

  • I am getting an index out of range error, when I initialize the TValue to 10. So the elements should exist.

     if (!sportsDictionary.ContainsKey(sports.Team))
        {
            sportsDictionary.Add(sports.Team, new List<SportsObject>(10));     
        }
         else
        {
            sportsDictionary[sports.Team][Team.sportsId] = sports; //Error: index out of range
        }
    }


    pianoboyCoder

    Sunday, July 31, 2016 1:50 AM

All replies

  • Buffer overflows have been a big issue in the past. As a result all .NET collection classes will check if the index you give it is valid. It is not.

    A common problem is that indexes start with 0, not 1. However to give you any real information we need to know what the indexes are at any given time.

    Sunday, July 31, 2016 1:57 AM
  • That's the thing because I have input values coming out of order, when I get the input values they have to be set at a specific array element that may not exist. So im trying to come up with a robust solution to account for that case.

    When the element does not exist, how can i ensure the elements exist so that i can add to a specific element.


    pianoboyCoder

    Sunday, July 31, 2016 4:16 AM
  • Better way to check sportsId is by object but not link to index.

    I would suggest

              if (!sportsDictionary.ContainsKey(sports.Team))
                {
                    sportsDictionary.Add(sports.Team, new List<SportsObject>(10));             
                }
                else
                {
                    var sportsObject = sportsDictionary[sports.Team].SingleOrDefault(x => x.Id == sports.sportsId);
                }

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, July 31, 2016 4:49 AM
  • So after I do this below then what. My goal is to add the incoming data to a specific element that may not exist in the list, what do I do in that scenario. The Sport Objects have to be in a certain order. But the objects aare not coming in order. How do i account for the element that does not exist below.


    sportsDictionary[sports.Team][0] // Exist

    sportsDictionary[sports.Team][1] // Exist

    sportsDictionary[sports.Team][2] // Exist

    sportsDictionary[sports.Team][3] // Exist

    sportsDictionary[sports.Team][7] // Does not Exist

      var sportsObject = sportsDictionary[sports.Team].SingleOrDefault(x => x.Id == sports.sportsId);



    pianoboyCoder

    Sunday, July 31, 2016 4:56 AM
  • simple, if it is not available you will add

    var team = sportsDictionary[sports.Team];
    
    var sportsObj = team.SingleOrDefault(x => x.Id == sports.sportsId);
    if(sportsObj == null)
    {
      team.Add(sports);
    }


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, July 31, 2016 7:26 AM
  • If I say team.Add(sports). It will just add another element to the list, which is not what i want to do. The data coming  inis out of order and it has to be at a specific index.

    For example 0-3 elements exist which will, but if the incoming coming has value of 7, I must put it in the sportsDictionary sports element 7, but it doesn't exist because element 4-6 have not been added. So what do i do for this scenario. The constraint is I cannot throw away valid data.

    sportsDictionary[sports.Team][7].Index

    sportsDictionary[sports.Team][0].Index =0 // Exist

    sportsDictionary[sports.Team][1].Index =1 // Exist

    sportsDictionary[sports.Team][2].Index =2// Exist

    sportsDictionary[sports.Team][3].Index =3 // Exist

    sportsDictionary[sports.Team][7].Index =7// Does not Exist


    pianoboyCoder

    • Proposed as answer by Hart Wang Friday, August 5, 2016 9:13 AM
    Sunday, July 31, 2016 8:02 AM
  • Basically I am asking is there a better way to do this below. This works so far when an element does not exist. Because incoming data have to be at a specific index. 
     
    if (!sportsDictionary[sports.Team].Any(w => w.Team == sportsObject.Team))
    {

    if (sportsDictionary[sportsObject.Team].Count < sportsObject.Index)
    {
    int resize = sportsObject.Index - sportsDictionary[Sports.Team].Count;

    for (int i = 0; i <= resize; i++)
    {
    sportsDictionary[Sports.Team].Add(new SportsObject());
    }
    }

    sportsDictionary[Sports.Team].Insert(waypoint.Index, sportsObject);

    }


    pianoboyCoder

    Sunday, July 31, 2016 8:26 AM