2017-11-20 3 views
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です。どうして?私は何が欠けていますか?

+1

あなたは実際にキャッチに落ちていますか?そして、たとえあなたがそこに行こうとしても、値はゼロにリセットされます。 – johnyTee

+1

'attempts ++'は_old_ valueを返し、値を1だけインクリメントします。したがって、あなたは常に再帰関数に0を渡します。これを '++ attempts'に変更するか、このようなコンテキストでこれを使用せず、単に関数呼び出しの外側でインクリメントしてください。 – Evk

答えて

7

attempts++刻みattemptsその後、はそれ再帰前に行います。

0

attempts++をに変更してみてください。

UPD:oopsが長すぎます。