2016-11-24 5 views
-1

私はこのプログラムでほぼ7時間働いていますが、本当に感謝してくれたら間違っています。プログラムは同じIDを2回受け入れることを想定していません。私の問題は、私がキュレーターIDを入力して名前を付けて保存するときです。同じIDをもう一度入力すると保存され、 "キュレーターID保存ありがとうございます"IDをファイルに一度保存する方法は?

デバッグなし
private void SaveCuratorBtn_Click(object sender, RoutedEventArgs e) 
    { 



     curator Curator = new curator(); 

     try 
     { 
      Curator.ID = CuratorIDbox.Text; 
      bool sameid = false; 
      for (int i = 0; i < curatorlist.Count; i++) 
      { 
       if (curatorlist[i].ID == Curator.ID) 
       { 
        sameid = true; 
        break; 
       } 
      } 
      if (sameid) 
       MessageBox.Show("ID already exist please try again !"); 
      else 
      { 
       if (string.IsNullOrEmpty(CuratorIDbox.Text) || string.IsNullOrEmpty(CuratorNamebox.Text)) 
        MessageBox.Show("please do not leave Boxes empty!"); 
       else 
       { 


        curatorlist.add(Curator); 
        savefile(); 
       } 
      } 



     } 


     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
     try 
     { 

      Curator.NAME = CuratorNamebox.Text; 
      MessageBox.Show("Curator Saved Thank You"); 
      savefile(); 
      CuratorIDbox.Text = ""; 
      CuratorNamebox.Text = ""; 

     } 

     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 
+1

デバッグなしで言うのは難しいです。私はtry/catchの中に生きていること以外のロジックには何も間違っているとは思わない。例外をスローすることが知られているものだけを試してみてください。 –

+0

メソッドへの入力時にキュレーターリストの内容を確認しましたか? – PaulF

+0

私の推測では、キュレーターリストは空です。 – SteppingRazor

答えて

1

、私はこのようなあなたのコードをリファクタリングします:

private void SaveCuratorBtn_Click(object sender, RoutedEventArgs e) 
{ 
    if (string.IsNullOrEmpty(CuratorIDbox.Text) || string.IsNullOrEmpty(CuratorNamebox.Text)) 
    { 
     MessageBox.Show("please do not leave Boxes empty!"); 
     return; // If one of the textboxes is empty, we don't continue executing the method 
    } 

    curator Curator = new curator(); 
    Curator.ID = CuratorIDbox.Text; 
    bool sameid = false; 

    for (int i = 0; i < curatorlist.Count; i++) 
    { 
     if (curatorlist[i].ID == Curator.ID) 
     { 
      sameid = true; 
      break; 
     } 
    } 

    if (sameid) 
    { 
     MessageBox.Show("ID already exist please try again !"); 
     return; 
    } 
    else 
    { 
     Curator.NAME = CuratorNamebox.Text; 

     // I suppose your savefile method can throw exceptions... this has to be in a try-catch block then 
     try 
     { 
      savefile(); 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
      return; 
     } 

     MessageBox.Show("Curator Saved Thank You"); 
     CuratorIDbox.Text = ""; 
     CuratorNamebox.Text = ""; 
    } 
} 

1つの提案:

if (curatorlist.Any(c => c == Curator.ID)) { ... } else { ... } 

は、私は次の行を使用する既存の学芸員の確認

(これとforループを交換できます)

+0

ちょうどそれを修正した人ありがとう – Shahzada

関連する問題