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 }
deltree
source share