求助,请教下面这个方法的逻辑是否合理
本帖最后由 yan_hyz 于 2014-01-17 10:31:26 编辑
/// <summary>
/// 将一个指定的组合图形拆分为多个图形,拆分后的子图形将被设置为【非锁定】和【非选中】状态。
/// </summary>
/// <param name="dGroup">指定要拆分的组合图形</param>
/// <returns>返回一个包含拆分后的子图形的图形列表</returns>
public static List<BaseElement> BreakObj(DrawGroup dGroup)
{
if (dGroup == null)
{
throw new Exception("请确保参数不为null。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
if (dGroup.children == null)
{
throw new Exception("请确保参数是一个有效的组合图形。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
if (dGroup.children.Count <= 0)
{
throw new Exception("请确保参数是一个有效的组合图形。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
for (int i = 0; i < dGroup.children.Count; i++)
{
if (dGroup.children[i] == null)
{
dGroup.children.RemoveAt(i);
i--;
continue;
}
dGroup.children[i].BLock = false;
}
if (dGroup.children.Count <= 0)
{
throw new Exception("请确保参数是一个有效的组合图形。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
List<BaseElement> eLst = new List<BaseElement>();
for (int i = 0; i < dGroup.children.Count; i++)
{
eLst.Add(dGroup.children[i]);
if (dGroup.children[i] is DrawSignItem)
{
//Do Samething
}
else
{
dGroup.children[i].resetLocation();
}
}
dGroup.locateToChild();
dGroup.children.Clear();
dGroup.Dispose();
for (int i = 0; i < eLst.Count; i++)
{
eLst[i].resetLocation();
eLst[i].BLock = false;
eLst[i].BIsSelected = false;
eLst[i].FGraphUnit = dGroup.FGraphUnit;
}
return eLst;
}
纠结好几天了,确定不下来。
[解决办法]
if (dGroup.children == null)
{
throw new Exception("请确保参数是一个有效的组合图形。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
if (dGroup.children.Count <= 0)
{
throw new Exception("请确保参数是一个有效的组合图形。方法名:[GroupAction.BreakObj];参数:[DrawGroup dGroup]");
}
我能不能理解为这两句表达的是一个意思
[解决办法]
for?(int?i?=?0;?i?<?dGroup.children.Count;?i++)
????????????{
????????????????if?(dGroup.children[i]?==?null)
????????????????{
????????????????????dGroup.children.RemoveAt(i);
????????????????????i--;
????????????????????continue;
????????????????}
????????????????dGroup.children[i].BLock?=?false;
????????????}
如果第1个记录就为空,不会死循环?
既然是空为什么还要去Remove?
有点不明白
[解决办法]
for (int i = 0; i < dGroup.children.Count; i++)
{
if (dGroup.children[i] == null)
{
dGroup.children.RemoveAt(i);
i--;
continue;
}
dGroup.children[i].BLock = false;
}
额,我们不知道你到底要怎么保证,所以只能给点技巧性提示,这个代码别扭点,这里有下技巧来着,不要正向循环,i++,i--看滴郁闷啊!这里你负向循环,从结尾开始判定移除,这样就不会影响上面的顺序了
[解决办法]
楼上的建议不错,不过都行,只要自己不被绕糊涂就成。看代码没什么太大的问题,有些判断可以通过
[解决办法]
合并起来,减少下代码。