Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have written a program in C# and I am briefing you the error.

I create zones (rectangular) on images on mouse drag (just like selecting files and folders in any Operating System).

So, I have a PictureBox_MouseDown where it shows me the following error:
'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'


It happens whenever I right click on the PictureBox. I want to get rid of such exceptions.

Please help.

Regards
Aman Chaurasia

What I have tried:

if (PictureBox1.Image != null)
            {
                if (ZonesItems != null)
                {
                    if (ZoneCreationOnProcess == true)
                    {

                        bool FalseZoneCreated = false;
                        if (ZonesItems[LastZoneCount].ZoneArea.Height <= 1)
                        {
                            ZonesItems.Remove(ZonesItems[LastZoneCount]);
                            FalseZoneCreated = true;
                        }
                        else if (ZonesItems[LastZoneCount].ZoneArea.Width <= 1)
                        {
                            ZonesItems.Remove(ZonesItems[LastZoneCount]);
                            FalseZoneCreated = true;
                        }
                        if (FalseZoneCreated == false)
                        {
                            if (ZonesItems[LastZoneCount].ZoneCssIdentity == "article-full-headline")
                            {
                                if (pdfArticlePageIndex != pdfPageIndex)
                                {
                                    pdfArticleIndex = 0;
                                    pdfArticlePageIndex = pdfPageIndex;
                                }
                                pdfArticleIndex = pdfArticleIndex + 1;
                                ToolStripArticleNo.Text = pdfArticleIndex.ToString();

                            }
                            else if (ZonesItems[LastZoneCount].ZoneCssIdentity == "article-in-image")
                            {
                                pdfPageImageIndex = pdfPageImageIndex + 1;
                            }
                            ZonesItems[LastZoneCount].ArticleNo = pdfArticleIndex;
                            LastZoneCount = LastZoneCount + 1;
                            if (LastZone == ZoneName.TextArea)
                            {
                                LastTextAreaCount = LastTextAreaCount + 1;
                            }
                            else if (LastZone == ZoneName.ImageArea)
                            {
                                LastImageAreaCount = LastImageAreaCount + 1;
                            }
                            else if (LastZone == ZoneName.TableArea)
                            {
                                LastTableAreaCount = LastTableAreaCount + 1;
                            }

                        }

                    }
                    else
                    {
                        if (ZoneSelected == true)
                        {
                            if (SelectedZoneCount != 0)
                            {

                            }
                            else
                            {

                            }
                        }
                        else
                        {

                        }
                    }
                }
                else
                {

                }
            }
        }


The bold syntax is where I am getting the error.
Posted
Updated 16-May-18 23:17pm
Comments
F-ES Sitecore 17-May-18 5:01am    
Before you access an element in an array you should check the array contains the required number of elements. If I have a three element array

a[0] = "a"
a[1] = "b"
a[2] = "c"

and I ask for a fourth element

string x = a[3]

that will throw the exception you are seeing as there is no fourth element. So you need to check the size of your array before addressing items inside it.

if (LastZoneCount >= ZonesItems.Length)
{
// trying to access an element that does exist
}

What you do in there depends on your business logic. You might just want to "return" to abort the process, you might want to show a warning, we don't know.
Primo Chalice 17-May-18 5:06am    
A warning is needed as anyone can right click by accident. So aborting is stupidity, I guess. How shall I implement this? Please help.
Richard MacCutchan 17-May-18 5:49am    
Either check your index values, before you use them, or use a try/catch block. It is your responsibility as the developer to ensure that user entered data is validated before you use it.

1 solution

Quote:
A warning is needed as anyone can right click by accident. So aborting is stupidity, I guess. How shall I implement this? Please help.
We can't - the first thing you have to do is use the debugger to find out exactly what is in the array index, and why it got there. Put a breakpoint on the line throwing the exception, and look at the variables when it gets hit. Think about what you expected to be there, and what you actually got. Why are they different? How did that value get there?

If it's a user error, then check for it, and display an appropriate error using MessageBox. If it isn't, then start back tracking how the value got there, and find out what you need to do to prevent that from happening again.

Sorry, but we can't do that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900