2013-11-20 15 views
0

我工作的C#項目,所以我有2個功能,我只需要如果FUNCTION1已經做到,那麼我想要的功能2執行,其他功能2,必須防止執行(Didnot Execute !!)。如果FUN1做做FUN2,否則不做FUN2

我的功能1名爲SaveDuplicatCourse()和功能2呼籲Save(),對方的代碼如下:

public void SaveDuplicatCourse() 
{ 
    if(con.State !=ConnectionState.Open) 
     con.Open(); 
    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
     IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 
    foreach (int i in IDs) 
    { 
     try 
     { 
     SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses 
              values(" + i + "," + CCID + ")", con); 
     com.ExecuteNonQuery(); 
     } 
     catch (Exception) 
     { 
     MessageBox.Show("They are exist"); 
     } 
    } 
    con.Close(); 
} 

public void Save() 
{ 
    SqlCommand com = new SqlCommand(@"Delete from students 
            where 
            Course_ID = " + ID, con); 
    con.Open(); 
    com.ExecuteNonQuery(); 

    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value!=null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
     IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 

    foreach (int i in IDs) 
    { 
     com.CommandText = "Insert into students values(" + i + "," + ID + ")"; 
     com.ExecuteNonQuery(); 
    } 
    con.Close(); 
} 

ButtonClick稱這些功能如下

private void buttonSaveChanges_Click(object sender, EventArgs e) 
{ 
    SaveDuplicatCourse(); 
    Save(); 
} 

請告訴我怎麼回事將會完成。

+3

我不知道你在說什麼。 'unexecuted'?什麼......? –

+3

使這些函數返回布爾值而不是void。 – MikeTheLiar

+1

第一個函數返回'bool'? if(Func1())Func2();' –

回答

1

向函數1添加boolean值。如果它返回false,請不要運行function2。如果它返回true,則運行function2。

boolean saveDuplicateCourseCompleted = false; 

private void buttonSaveChanges_Click(object sender, EventArgs e) 
{ 
    if (saveDuplicateCourseCompleted == true) 
     Save(); 
    else 
     // or do something else.   
} 

注意下面的例子:

public boolean SaveDuplicatCourse() 
{ 
    if(con.State !=ConnectionState.Open) 
     con.Open(); 
    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
      IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 
    foreach (int i in IDs) 
    { 
     try 
     { 
      SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses 
      values(" + i + "," + CCID + ")", con); 
      com.ExecuteNonQuery(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("They are exist"); 
      saveDuplicateCourseCompleted=false// Now this is right way 
     } 
    } 
    con.Close(); 

} 
+0

現在它的工作如此美好,^ _ ^謝謝 –

1

在你的榜樣,我看不出是異步發生什麼,所以你的代碼應該在你正在尋找時尚的執行。現在,如果你問的是成功執行(而不是任何不成功的可能,即沒有行被刪除),這是一個不同的故事,並且可以用各種不同的方式來處理。但在我們走下這條路之前,似乎你已經獲得了理想的結果。設置一個斷點並遍歷每行代碼以觀察執行順序。

+0

對不起,但我的代碼不執行,因爲我想 –

相關問題