Select to view content in your preferred language

Index was out of range. Must be no-negative and less than the size of collection.

1300
3
01-18-2012 05:41 AM
QaisarKhyber
Emerging Contributor
Hello!

I have made a webmap with functionalities like basic toolbar (zoom in/out, print out etc), find information and search in map by writing an address.
When I search in my map by writing address and get a list of possible addresses, I can select any post to get it zoomed in my map without any problem. My ???Find information??? function works fine too but when getting back to the address list (searched earlier) if I try to pick any post (from 2nd to the last in the list) it gives an error ???Index was out of range. Must be no-negative and less than the size of collection. Paranetername: index??? at this code line "MapPoint candidatePoint = _candidateGraphicsLayer.Graphics[index].Geometry as MapPoint;"
This happens only when both find an address and find information are working at the same time.

Code is
GraphicsLayer _candidateGraphicsLayer;

void _candidateListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = (sender as ListBox).SelectedIndex;
if (index >= 0)

{
MapPoint candidatePoint = _candidateGraphicsLayer.Graphics[index].Geometry as MapPoint;

double displaySize = MyMap.MaximumResolution * 0.27;

ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
candidatePoint.X - (displaySize / 2),
candidatePoint.Y - (displaySize / 2),
candidatePoint.X + (displaySize / 2),
candidatePoint.Y + (displaySize / 2));

MyMap.ZoomTo(displayExtent);
}
}

Any solution?

Thanks in advance!
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
I'm not sure how you are populating the ComboBox.Items to retrieve index. But how about the including the following condition:
if(index >= 0 && _candidateGraphicsLayer.Graphics.Count > index)
0 Kudos
QaisarKhyber
Emerging Contributor
Hello Jennifer!

Thanks for replying. By adding this condition I don�??t get any error but this disables the functionality of address List. Nothing happens when I click on any post. My find an address code looks like this;

private void LocatorTask_AddressToLocationsCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs args)
        {
            _candidateGraphicsLayer.ClearGraphics();
            CandidateListBox.Items.Clear();
            List<AddressCandidate> returnedCandidates = args.Results;
            foreach (AddressCandidate candidate in returnedCandidates)
            {
                if (candidate.Score >= 80)
                {
                    CandidateListBox.Items.Add(candidate.Address);

                    Graphic graphic = new Graphic()
                    {
                        Geometry = candidate.Location
                    };
                    graphic.Attributes.Add("Address", candidate.Address);
                    string latlon = String.Format("{0}, {1}", candidate.Location.X, candidate.Location.Y);
                    graphic.Attributes.Add("LatLon", latlon);

                    if (candidate.Location.SpatialReference == null)
                    {
                        candidate.Location.SpatialReference = new SpatialReference(3008);
                    }
                    if (!candidate.Location.SpatialReference.Equals(MyMap.SpatialReference))
                    {
                        if (MyMap.SpatialReference.Equals(new SpatialReference(10210)) && candidate.Location.SpatialReference.Equals(new SpatialReference(3008)))
                            graphic.Geometry = _mercator.FromGeographic(graphic.Geometry);
                        else if (MyMap.SpatialReference.Equals(new SpatialReference(3008)) && candidate.Location.SpatialReference.Equals(new SpatialReference(10210)))
                            graphic.Geometry = _mercator.ToGeographic(graphic.Geometry);
                        else if (MyMap.SpatialReference != new SpatialReference(3008))
                        {
                            GeometryService geometryService = new GeometryService("http://<servername>/ArcGIS/rest/services/Geometry/GeometryServer");

                            geometryService.ProjectCompleted += (s, a) =>
                            {
                                graphic.Geometry = a.Results[0].Geometry;
                            };

                            geometryService.Failed += (s, a) =>
                            {
                                MessageBox.Show("Projection error: " + a.Error.Message);
                            };
                            geometryService.ProjectAsync(new List<Graphic> { graphic }, MyMap.SpatialReference);
                        }
                    }

                    _candidateGraphicsLayer.Graphics.Add(graphic);
                }
            }

            if (_candidateGraphicsLayer.Graphics.Count > 0)
            {
                CandidatePanelGrid.Visibility = Visibility.Visible;
                //this.CandidateListBox.Visibility = Visibility.Visible;
                CandidateListBox.SelectedIndex = 0;
            }
        }

        void _candidateListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = (sender as ListBox).SelectedIndex;
            if (index >= 0)
            {
                MapPoint candidatePoint = _candidateGraphicsLayer.Graphics[index].Geometry as MapPoint;

                double displaySize = MyMap.MaximumResolution * 0.27;

                ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                    candidatePoint.X - (displaySize / 2),
                    candidatePoint.Y - (displaySize / 2),
                    candidatePoint.X + (displaySize / 2),
                    candidatePoint.Y + (displaySize / 2));

                MyMap.ZoomTo(displayExtent);
            }
        }
0 Kudos
JenniferNery
Esri Regular Contributor
If
_candidateGraphicsLayer.Graphics.Count > index
condition fails, then index does not match your GraphicsCount. You have to debug through your code to know if _candidateGraphicsLayer.Graphics.Add() is hit and what value index is when it tries to access _candidateGraphicsLayer.Graphics.
0 Kudos