Out of Range Index Error Prevention

I want to write a check for some conditions without using try / catch, and I want to avoid the possibility of getting index errors out of range

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there at least one Object array that has values { if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty { // execute code here } } 

So the problem I am facing is that in the second check I need to find out if I have one element that is not empty. However, if I do not have Element[1] , I get an Index Out of Range exception. The problem is that there can be 2 elements, and one (or both) of them can have empty arrays of objects. The code should only be executed if one of the Item lines is not empty.

Hope I explained it well. How can I avoid getting this exception under any conditions?

+7
source share
6 answers

Ok, you need a better null check and more careful code here.

 if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there at least one Object array that has values { if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty { // execute code here } } 

just unacceptable.

First, let null check

 if (array != null) { if (array.Element != null) 

for simplicity you can use &&

 if (array != null && array.Element != null) 

then inside this if we use a for loop ( since you are stuck on arrays ) and null check it

 for (int i = 0; i < array.Element; ++i) { if (array.Element[i] != null && array.Element[i].Object != null) { 

then, since you have nested arrays, we are looping again. This is called a nested loop , and it is usually a bad practice, I will show you why it works in a second.

 for (int o = 0; o < array.Element[i].Object.length; ++o) { if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item)) { 

Now, with all this ugly nested loop, we figured out that your Item is not null. In addition, you have access to all potential values ​​here and can group them as you wish. Here, how would I put it all together for simplification.

 List<string> arrayValues = new List<string>(); if (array != null && array.Element != null) { for (int i = 0; i < array.Element.length; ++i) { //bool found = false; if (array.Element[i] != null && array.Element[i].Object != null) { for (int o = 0; o < array.Element[i].Object.length; ++o) { if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item)) { arrayValues.Add(array.Element[i].Object[o].Item); //if you want to drop out here, you put a boolean in the bottom loop and break and then break out of the bottom loop if true //found = true; //break; } } } //if (found) // break; } } if (arrayValues.Count > 0) { //do stuff with arrayValues } 
+3
source

Place both tests together using the && short circuit so that the second test does not occur if the first fails:

 object element0 = array.Element[0].Object; object element1 = array.Element[1].Object; // Ensure at least one Object array has a non-empty value. if ((element0.Length > 0 && !string.IsNullOrEmpty((string)element0.Object[0].Item)) || (element1.Length > 0 && !string.IsNullOrEmpty((string)element1.Object[1].Item))) { // ... } 

I assume that this Object[1] throws an exception - you did not understand this. If this Element[1] throws an exception (or both), you must first check the length of the array:

 if ((array.Element[0].Length != 0 && HasValue(array.Element[0].Object)) || (array.Element[1].Length > 1 && HasValue(array.Element[1].Object))) { // ... } // <summary> // Returns true if the specified string array contains a non-empty value at // the specified index. // </summary> private bool HasValue(System.Array array, int index) { return array.Length > index && !string.IsNullOrEmpty((string)array.Object[index].Item); } 
+1
source

Could you do something like:

 if(array.Element[0] != null || array.Element[1] != null){ //execute code } 
0
source

Your code probably depends on refactoring.

I assume it can work as follows:

 var obj = array.Element.FirstOrDefault(x => x.Object.Length > 0); if (obj != null) { if (obj.Object[0].Item.Length != 0) { // do whatever is necessary } } 
0
source

I think you can put your check right in front of the first if Check.

 if (array.Length > 1 && array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there at least one Object array that has values { if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty { // execute code here } } 

This should be a short circuit if your array does not have both elements.

0
source
 for (long i = 0; i <= (output3.Length); i++) { output1.WriteByte(output3[i]); -----> index out of range exception correct it output1.WriteByte(output3rx[i]); } 
0
source

All Articles