1. This forum is in read-only mode.

Some general C# coding help

Discussion in 'Computers & Modding' started by Reider, Nov 28, 2011.

  1. Reider

    Reider Modereider

    I've been working on this program that reads text files. I've got it all hammered out except for this one little snag.

    I've got two radio buttons(aptly labeled radAll and radHundred). I've been having a little snag with radHundred though.

    The thing is that whenever I have radHundred checked and I read a text file that happens to be under 100 lines, it gives me an unhandled exception error that looks like this:

    [​IMG]

    I've included all the code for radHundred. I think it has something to do with how I wrote the loop but I'm not quite sure how to fix it at the moment. Probably gonna be one of those things I figure out just before somebody responds. :p


    Code:
                    //Checking if 100 lines is checked
                    else if (radHundred.Checked == true)
                    { //loop for if "100 lines" radio button is clicked
                        do
                        {
                            Shaky = Reider.ReadLine();
                            count++;
                            listFile.Items.Add(count + "   " + Shaky);
                            
    
                        //Declaring the variable for any matches in the text against txtIn.Text
                        int loc = Shaky.IndexOf(txtIn.Text);
                        //The if statement to show the matches in the listMatches listbox
                        if (loc != -1)
                        {
                            match++;
                            listMatches.Items.Add("Was located on line " + count.ToString() + "  " + Shaky);
                        }
                       } while (count <= 100);
                        
                        //Total Number of lines in the text for listStats on the Stats tab
                        listStats.Items.Add("Total Number of lines: " + count.ToString());
                        
                        //Total Number of Matches in the text for listStats on the Stats tab
                        listStats.Items.Add("Total Number of Matches: " + match.ToString());
    
    }
    
    If you need the whole thing to get an idea of what's going on, let me know.
     
  2. calvin_0

    calvin_0 Well-Known Member

    i been out of touch with C for almost 10 years now, but dont indexof return a string value? so maybe changing your variable loc from integer to string.

    but again i could be wrong about this.
     
  3. Reider

    Reider Modereider

    That integer was for seeding out the lines that didn't have matches to the string I typed into a text box. It would've displayed the entire text instead of just the lines which had whatever characters I typed in if I didn't set it up that way.

    I figured out what the problem was. I just needed to add an extra condition to the loop. No idea why it never occurred to me to add another condition but ah well.