Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (coupleSubstrings.Count > 1)
      {
          for (int i = 0; i<coupleSubstrings.Count-1; i++)
              coupleTemp[i] = coupleSubstrings[i] +" "+ coupleSubstrings[i + 1];

          dt.Clear();
          if (!ViewReqInfo(dt, coupleTemp.ToArray()))
          {
              lblErrorMessage.CurrentValue = objWord.ErrMessage;
          }
          if (dt.Rows.Count > 0)
          {
              foreach (DataRow dr in dt.Rows)
              {
                  value = dr["IsInsult"].ToString();
                  if (value == "True")
                  {
                      lblErrorMessage.CurrentValue = string.Empty;
                      lblErrorMessage.CurrentValue = "این جمله شامل کلمات غیر مجاز می باشد.";
                      break;
                  }
                  else
                  {
                      lblErrorMessage.CurrentValue = string.Empty;
                      lblErrorMessage.CurrentValue = "این جمله نیازمند بررسی بیشتر است.";
                      txtEntry.CurrentValue = string.Join(" ", coupleTemp);
                  }
              }
          }
      }
  }


What I have tried:

This method does not work the array length is longer but I dont know what is the problem
Posted
Updated 16-Feb-18 22:07pm

Look at your code:
for (int i = 0; i<coupleSubstrings.Count-1; i++)
          coupleTemp[i] = coupleSubstrings[i] +" "+ coupleSubstrings[i + 1];
The loop runs i from to the last element in the array, but you then access that element plus one - which by definition does not exist.
 
Share this answer
 
Comments
phil.o 17-Feb-18 3:28am    
I read it several times to be sure, array is indexed until the last but one element (i < coupleSubstrings.Count - 1).
The problem has to be with the coupleTemp array, since the coupleSubstrings one is properly indexed. Would you mind showing the declaration of the coupleTemp array? It appears you did not provide it with enough elements (ideally, it should have one element less than the coupleSubstrings array).
 
Share this answer
 
Quote:
Index out of range exception for array

Along with the error message, you have also been told the position of error, would be nice to share. Since code snipset is not autonomous, it is difficult to guess what is what.
Quote:
This method does not work the array length is longer but I dont know what is the problem

Since arguing with computer that your code is right will not help, you have to find another way. This other way is to run your code with debugger, and when error occurs, inspect variables and array to understand what your code is doing and why computer reject it.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
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