Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# index out of range String Array and List<string>

Tags:

arrays

c#

After trying a few error checking methods I have come to the conclusion I need help solving this problem.

How am I not catch this "index out of range" error. What can I do to avoid this problem in the future for good practice?

    public void loadFromFile()
    {
        OpenFileDialog oFile = new OpenFileDialog();
        oFile.Title = "Open text file";
        oFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        oFile.FilterIndex = 1;
        oFile.InitialDirectory = Application.StartupPath;
        oFile.AddExtension = true;
        oFile.CheckFileExists = true;
        oFile.CheckPathExists = true;

        // Open and clean Duplicates
        String[] lines;
        List<string> temp = new List<string>();
        List<string> newlist = new List<string>();

        if(oFile.ShowDialog() == DialogResult.OK)
        {
            // Dummy file has 6 lines of text. Filename:DuplicatFile.txt
            // 3 duplicate lines and 3 not.
            lines = File.ReadAllLines(oFile.FileName, System.Text.Encoding.UTF8);

            // Copy array to temporary array
            for (int index=0; index < lines.Length; index++)
            {
                // System.ArgumentOutOfRangeException was unhandled
                // Index was out of range. Must be non-negative and less than the size of the collection.
                if (lines[index].Length >= 0)
                {
                    temp[index] = lines[index];
                }
            }
            // Check for duplicates. If duplicate ignore if non-duplicate add to list.
            foreach (string line in temp)
            {
                if (!newlist.Contains(line))
                {
                    newlist.Add(line);
                }
            }
            // Clear listbox and add new list to listbox.
            lstBox.Items.Clear();
            foreach (string strNewLine in newlist)
            {
                lstBox.Items.Add(strNewLine);
            }
        }
    }
like image 722
Nightforce2 Avatar asked Dec 18 '10 06:12

Nightforce2


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

List<string> temp = new List<string>();
...
temp[index] = lines[index];

temp starts out with 0 size. Any index is out of range.

You can fix this by using temp.Add, to make the list grow dynamically:

temp.Add(lines[index]);
like image 146
Mud Avatar answered Nov 05 '22 21:11

Mud