2017-02-01 1 views
0

のbooleanメソッドの値を返すわけではありません。プログラミングではとても新しいので、Visual StudioでWindowsフォームアプリケーションを使用してパスワード管理プログラムを作成しようとしています。基本的には、これでアカウントを追加、編集、保存することができます。すべてのコードパスがクラス

私はAccountクラスを追加しました。私はaddAccountメソッドを使用しました。しかし、それは私に、すべてのコードパスが値を返すとは限らないというエラーを出しました。なぜこれはそうですか?私と一緒にゆっくり行ってください。私はこれで非常に新しいです。

namespace thePass 
{ 
    class Account 
    { 
     int numAccount; 
     string[] AccountName; 
     string[] Email; 
     string[] Username; 
     string[] Password; 

     //constructors  
     public Account(string in_AccountName, string in_Email, string in_Username, string in_Password) 
     { 
      numAccount = 0; 
      AccountName = new string[] { in_AccountName }; 
      Email = new string[] { in_Email }; 
      Username = new string[] { in_Username }; 
      Password = new string[] { in_Password }; 
       //NOT DONE KEEP GOING HERE CHECK NOTES FOR HELP maybe i leave as default? 
     } 

     public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
     { 
      bool isFound = false; 
      bool isAdded = false; 
      for (int i = 0; i < numAccount; i++) 
      { 
       if (AccountName[i].CompareTo(in_Account) == 0) 
       { 
        isFound = true; 
        break; 
       } 
       if (isFound == false) 
       { 
        AccountName[numAccount] = in_Account; 
        numAccount++; 
        isAdded = true; 
       } 
       Email[numAccount] = in_Email; 
       Username[numAccount] = in_Username; 
       Password[numAccount] = in_Password; 
       return isAdded; 

      } 
     } 
+2

あなたのaddAccount関数はブール値を返さなければなりません。 numAccountが0の場合、forループは入力されません。したがってエラーです。一言で言えば、ループの外側にもブール値を返すべきです: – A3006

+1

ビジネス知識とコードの知識を混同しないように注意してください。たとえば、numAccountが決して '0'にならないことが分かります。コードはそうではありません。 'numAccount <= 0'の場合、コードは決して' return'に達しません。 'numAccount <= 0'が本当に有効でない場合は、それをチェックして例外をスローします。それにかかわらず、メソッドの最後にデフォルト値を返します。 BTW: 'for'ループ内で返す代わりに、ループの中から' break 'してメソッドの最後に単一の 'return isAdded'を持たせることができます。 –

答えて

3

for構造の中にあなたの返品があります。それは条件によってはアクセスできない場合があります。あなたはこれを行うことによって、これを修正できます:あなたが確信しているという理由だけで

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
    { 
     bool isFound = false; 
     bool isAdded = false; 
     for (int i = 0; i < numAccount; i++) 
     { 
      if (AccountName[i].CompareTo(in_Account) == 0) 
      { 
       isFound = true; 
       break; 
      } 
      if (isFound == false) 
      { 
       AccountName[numAccount] = in_Account; 
       numAccount++; 
       isAdded = true; 
      } 
      Email[numAccount] = in_Email; 
      Username[numAccount] = in_Username; 
      Password[numAccount] = in_Password; 


     } 
     return isAdded; 
    } 

(あなたがプログラミングの初心者だった述べたので、ちょうどこれを追加すること)されて実現するために重要なこと、このような条件(あなたのロジック与えられました)コンパイラはこれを知る方法がありません。簡単に言えば、ここでのコンパイラのタスクは、データの異常が何であるかにかかわらず、いずれかの方法で実行パスが何であってもboolの値を返すように強制することです。方法

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
{ 
    ... 
    for (int i = 0; i < numAccount; i++) 
    { 
     ... 
     return isAdded; 
    } 
} 

0

addAccountboolを返す必要がありますが、問題は、あなたのループを実行しないことです。ルーピング条件はfor (int i = 0; i < numAccount; i++)です。 numAccountが、たとえば-1のように始まるとどうなりますか?

ループのの外側にisAddedを返す必要があります。

return isAdded; 

ループの終了括弧の下に移動してください。

0

あなたは、ループ内で値を返しますが、あなたはエラーがある、ループの外で値を返しません。

あなたはコードを低くして、ループで土地を返します。 私はされるべきだと思う:

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
{ 
    ... 
    for (int i = 0; i < numAccount; i++) 
    { 
     ... 
    } 

    return isAdded; 
} 
0

あなたの部分で、このcode.The問題を試してみては、あなたがのためにループ内で値を返すされていることです。コンパイラは、あなたが正しいロジックを書いた天気を知る方法がなく、コードを実行しない限り、そうしないといけません。また、 'return'の種類は、それぞれのブロックまたはメソッドの終わりを表します。したがって、あなたの各条件ごとに値を返すことはできません。だからそれはちょうど偽、したがってコンパイル時のエラー。

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
    { 
     bool isFound = false; 
     bool isAdded = false; 
     for (int i = 0; i < numAccount; i++) 
     { 
      if (AccountName[i].CompareTo(in_Account) == 0) 
      { 
       isFound = true; 
       break; 
      } 
      if (isFound == false) 
      { 
       AccountName[numAccount] = in_Account; 
       numAccount++; 
       isAdded = true; 
      } 
      Email[numAccount] = in_Email; 
      Username[numAccount] = in_Username; 
      Password[numAccount] = in_Password;     
     } 
     return isAdded; 
    } 
0

あなたがforループ内のブール値を返すされているが、この方法は、ループのために入っていない場合、どのvalue.thisを返すことはできませんが、あなたのケースで起こっです:では

numaccount = 0

これで、numaccountに値を代入していないため、0、 というメソッド呼び出しでは、forループを入力することはありません。As

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password) 
    { 
     bool isFound = false; 
     bool isAdded = false; 
     for (int i = 0; i < numAccount; i++) // here i = numAccount = 0, hence this for loop will never execute 
     { 

      /* rest codes*/ 

       return isAdded; 

     } 

     /* No return statement added here, hence the error*/ 
    } 
関連する問題