嘿,我的情況是,我有一個執行某些操作的 for 回圈,我想撰寫一行代碼,要么呼叫傳入由 for 回圈索引索引的陣列的函式,要么運行單個(非陣列)變數對于該函式的每次呼叫,我知道我可以通過在 for 回圈中放置一個 if 陳述句來做到這一點,但我會一遍又一遍地重復相同的 if 陳述句,以獲得相同的結果。那么有沒有一種好方法可以在 for 回圈之前運行 if 陳述句,并且該 if 陳述句的結果運行相同的 for 回圈,但是一個呼叫傳入陣列或變數?
代碼示例
for (int i = 0; i < CurrentVerticalList.Count; i )
{
GuiGeneral CGroup = CurrentVerticalList[i];
CGroup.ResizeUsingStandard(ForcedResize[i]); //I want the condition before the for
//loop to have ForcedResize[i] here if
//true and another variable here of the
//same type but not an array if false.
for (int j = 0; j < 2; j )
{
GlobalListIndex[j] ;
}
CGroup.MoveElementTo(CCoord, false);
CCoord.y = CGroup.ElementRect.WidthHeight.y;
}
uj5u.com熱心網友回復:
呃,我很難理解這個問題,但我認為你想要的是這些方面的東西,可以幫助提供更多型別,但你可以使用本地函式實作意圖:
ForcedResizeArrayType other = new object(); //TODO: Define return type
bool condition = ResolveCondition(); //TODO: Define condition to be true or false
ForcedResizeArrayType GetOneOr(int i, bool condition,
ForcedResizeArrayType[] forcedResizeArray)
{
return condition ? forcedResizeArray[i] : other;
}
for (int i = 0; i < CurrentVerticalList.Count; i )
{
CGroup.ResizeUsingStandard(GetOneOr(i, condition, ForcedResize));
}
如果我喜歡或討厭這些本地功能,每周都會有所不同,但它們有用途
uj5u.com熱心網友回復:
在這里,將條件檢查移出 for 回圈:
Func<int, double> GetResizeFromForcedResize = (index => ForcedResize[index]);
Func<int, double> GetResizeFromVariable = (index => fixVariable);
var GetResizeValue = condition? GetResizeFromForcedResize : GetResizeFromVariable;
for (int i = 0; i < CurrentVerticalList.Count; i )
{
GuiGeneral CGroup = CurrentVerticalList[i];
CGroup.ResizeUsingStandard(GetResizeValue(i));
for (int j = 0; j < 2; j )
{
GlobalListIndex[j] ;
}
CGroup.MoveElementTo(CCoord, false);
CCoord.y = CGroup.ElementRect.WidthHeight.y;
}
編輯:想讓您知道這里的其他答案仍在每次迭代時進行檢查,但不是這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317068.html
上一篇:從兩行獲取字串的正則運算式模式
下一篇:JS解密方法轉C#
