2012-04-11 58 views
7

我想写一些条件进行检查,而不必使用try/catch语句,我想避免让指数超出范围错误防止指数超出范围的错误

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's 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 
     } 
} 

所以这个问题我的可能性我面临的是,在第二次检查中,我需要看看我是否有一个不是空的物品。但是,如果我没有Element[1],则会出现索引超出范围异常。问题是可能有2个元素,其中一个(或两者)可能有空的对象数组。只有当其中一个项目字符串不为空时,代码才必须执行。

希望我解释得很好。我该如何避免在任何情况下获得这种例外情况?

+2

为什么不使用'名单',而不是一个数组? – 2012-04-11 20:48:07

+1

第一行中的两个条件是相同的 - 大概这不是故意的? – 2012-04-11 20:48:07

+0

我道歉,应该有两个不同的条件,我改变了它。 – Victor 2012-04-11 20:49:43

回答

3

好吧,你需要一些更好的null checking和一些更谨慎的代码在这里。

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's 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 
    } 
} 

是不可接受的。

首先,让我们空检查

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

为简单起见,你可以使用&&

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

话,里面的是,如果我们使用一个for循环(since you're stuck on arrays)和NULL检查

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

那么,既然你有嵌套数组,我们循环再次。这被称为nested loop,这通常是不好的做法,我会告诉你为什么它在一秒钟内工作。

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)) 
    { 

现在,所有这种丑陋的嵌套loopyness,我们发现你的物品不是空的。 最重要的是,您可以在这里访问所有潜在的值,并且可以按照自己的喜好进行分组。以下是我如何将整个事情放在一起以简化。

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 
} 
+1

做得很好!当我开始需要这样的代码时,当我开始思考我的设计是否合适。我怀疑OP的设计会受益于一些根本性的变化。 – 2012-04-11 21:22:57

0

你能不能做这样的事情:

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

我认为如果'Element [1]'不存在,这仍然会得到这个异常。 – Victor 2012-04-11 20:51:49

+0

啊,我相信你是对的。 – Ryan 2012-04-11 21:00:24

0

您的代码可能是受到重构。

我认为它可以以这种方式工作:

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

将两个测试使用短路&&在一起,因此,如果第一个失败,不会发生第二次测试:

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))) 
{ 
    // ... 
} 

我假设它是Object[1]引起的例外 - 你不清楚这一点。如果是Element[1]引起异常(或两者),那么你需要预先测试数组的长度:

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); 
} 
0

我想你可以把你的支票就在您的第一次,如果检查前。

if (array.Length > 1 && array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's 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 
     } 
} 

如果你的数组没有两个元素,这应该只是短路。

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

“索引超出范围的异常纠正它”。呃,他在问怎么做。 – 2013-10-16 09:13:55