0
私はこのコードを持っている:奇妙な行動
public void myFunction(String label, String type, string command, int attempts = 0)
{
try
{
Utility.Logger("myFunction attempt " + attempts.ToSafeString() + " label " + label + " type " + type, command);
...stuff...
}
catch (Exception e)
{
if (attempts < 10)
return myFunction(label, type, command, attempts++);
else
return null;
}
}
あなたが見ることができるように、私はキャッチブランチで再帰呼び出しを持って、私はパラメータにカウンタ=カウンタ+ 1
を設定奇妙な点は私のログでは常に試行= 0です。どうして?私は何が欠けていますか?
あなたは実際にキャッチに落ちていますか?そして、たとえあなたがそこに行こうとしても、値はゼロにリセットされます。 – johnyTee
'attempts ++'は_old_ valueを返し、値を1だけインクリメントします。したがって、あなたは常に再帰関数に0を渡します。これを '++ attempts'に変更するか、このようなコンテキストでこれを使用せず、単に関数呼び出しの外側でインクリメントしてください。 – Evk