0

I got function for finding word in text with indexof its returning list of objects with fields word and quantity,i want to use that function for different text and words to so im creating task and getting IndexOutOfRange on that line :

var r = Check_text(tnw.text[i], word);

For more understand thats my function:

    public static List<wordsinf> Check_text(string text,string[] words)
    {
        List<wordsinf> result = new List<wordsinf>();
        var pos = 0;
        var quantity = 0;
        foreach (string wf in words)
        {
            pos = 0;
            quantity = 0;
            while (true)
            {
                var foundPos = text.IndexOf(wf, pos);
                if (foundPos == -1)
                {
                    break;
                }
                pos = foundPos + 1;
                if (foundPos >= 0)
                {
                    quantity++;
                }
            }
            result.Add(new wordsinf(wf, quantity));
        }
        return result;
    }

thats example of inputs:

  -     word    {string[2]} string[]
            [0] "asd"   string
            [1] "qwe"   string
    -       tnw.text    {string[2]} string[]
            [0] "asd qwe ssd www "  string
            [1] "asd asd qwe sss "  string

Can anyone tell me solution for this problem?and what am i doing wrong there. Also there is part with tasks:

 var numtasks = tnw.text.Length;
        AnalyzeObj[] analyzeobjs = new AnalyzeObj[numtasks];
        var word = tnw.words.Split(',');
        Task[] tasks = new Task[numtasks];
        Console.WriteLine(word);
        Console.WriteLine(tnw.text);
       for (var i = 0; i < numtasks; i++)
        {
            tasks[i] = new Task(() => {
                var r = Check_text(tnw.text[i], word);
                analyzeobjs[i].text = tnw.text[i];
                analyzeobjs[i].WordInfos=r;
                analyzeobjs[i].id=Guid.NewGuid();
                analyzeobjs[i].FindWords = word;
            });
            tasks[i].Start();
         }
        Task.WaitAll(tasks);
vtarbinskyi
  • 85
  • 1
  • 1
  • 11
  • Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Gilad Green Aug 08 '16 at 11:43
  • 2
    Change `for (var i = 0; i < numtasks; i++)` to `for (var i = 0; i < numtasks-1; i++)` – 3615 Aug 08 '16 at 11:43
  • well i just tried your for and now i got 2 other exceptions: System.ArgumentException-as i understood it means like one of tasks is null and another exception is like System.NullReferenceException – vtarbinskyi Aug 08 '16 at 11:55

2 Answers2

1

The problem is that the Lambda Expression will not copy the value of the parameter but just use that exact variable instead. So after your Loop is over, i will be larger then the array length and therefor throw an exception.

To solve the problem, do the following in the for loop instead:

tasks[i] = new Task(new Action<object>((val) => {
            var c = (int)((object[])val)[1];
            var t = ((object[])val)[0].ToString();
            var r = Check_text(t, word); //Use the parameter val instead
            analyzeobjs[c].text = t; //Use the parameter val instead
            analyzeobjs[c].WordInfos=r;
            analyzeobjs[c].id=Guid.NewGuid();
            analyzeobjs[c].FindWords = word;
        }), new object[2] { tnw.text[i], i });
tasks[i].Start();
TheCodingDamian
  • 443
  • 2
  • 12
1
   for (var i = 0; i < numtasks; i++)
        {
            var i1 = i;
            tasks[i] = new Task(() =>
            {
                var item = new AnalyzeObj();
                var r = Check_text(tnw.text[i1], word);
                item.text = tnw.text[i1];
                item.WordInfos = r;
                item.id = Guid.NewGuid();
                item.FindWords = word;
                analyzeobjs.Add(item);
            });
            tasks[i1].Start();
        }
vtarbinskyi
  • 85
  • 1
  • 1
  • 11