2017-07-04 118 views
-1

關於我的計劃:我布爾值,即使所有的代碼是正確的假

我的算法(這類)是爲了檢查交付是否已完成,事後提供的用於卡車/拖車/驅動器另一種交貨,同時該算法發送另一輛貨車/拖車/司機。概括起來這個類執行以下操作:

  • 檢查預約是否在分配模式(分配模式基本上意味着「預訂」是在被交付
  • 檢查進度是否有噸位左預訂(例如,我想向一個地方發送500噸,但一次只能交付30噸,因此它會檢查是否還有噸需要交付)
  • 要使驅動程序(s) /卡車/拖車完成交貨
  • 自動分配噸位給司機/拖車/卡車
  • 刪除已完成的條目(任何已完成的預訂 - 日誌仍保留在數據庫中)。

我的問題:

我不知道什麼是錯我的課,我在這過小時,似乎無法找出是什麼原因造成我的布爾變量(forLoopBreak)觸發當它調用方法checkAvailTrailers()時,它的值爲「false」。問題似乎在那裏,但我不知道是什麼原因造成的問題。

類:

[https://pastebin.com/4up8eppd][1] 

筆記(我能爲我遇到字符的限制不能在這裏貼):

  • 我知道我的編程的外貌,但,但我我對此仍然陌生。
  • 我決定附上整個班級,因爲問題可能在不同的地方。

編輯:

我的代碼是太大,所以這裏探討的是有關部分:

private void startAlgo() 
{ 

        checkAvailTrailers(); 
         if (forloopBreak == false) 
         { 
          setErrorMessage("Avail Trailers not enough!"); 
         } 
} 

私人無效checkAvailTrailers()
{

string trailerRouteAllocation = null; 
    string trailerVragAllocation = null; 
    string tempHolderTrailer = null; 
    string myNewTempT = null; 
    string trailermyTemp = null; 

    int trailerCount = 0; 
    int tempTra = -1; 

    //Gets trailer route classification 
    using (SqlCommand selectTrailer = new SqlCommand("SELECT [TR_Routes] FROM dbo.TrailerDetail WHERE [TR_Allocation] = " + 0, con)) 
    { 
     using (SqlDataReader reader = selectTrailer.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       trailerRouteAllocation = reader.GetString(0); 
       tempHolderTrailer = trailerRouteAllocation; 
       myNewTempT = trailerRouteAllocation; 

       for (int l = 0; l < tempHolderTrailer.Length; l++) 
       { 
        tempTra = myNewTempT.IndexOf(","); 
        if (tempTra >= 0) 
        { 
         trailermyTemp = myNewTempT.Substring(0,tempTra); 
        } 
        else 
        { 
         trailermyTemp = myNewTempT; 
         if (trailermyTemp == myCurrentBookingRoute.ToString()) 
         { 
          mycurrentTrailerAvailableRoute[trailerCount] = tempHolderTrailer; 
         } 
         break; 

        } 
        myNewTempT = myNewTempT.Substring(tempTra + 1); 
        if (trailermyTemp == myCurrentBookingRoute.ToString()) 
        { 
         mycurrentTrailerAvailableRoute[trailerCount] = trailerRouteAllocation; 
        } 
       } 
       trailerCount++; 
      } 
      reader.Close(); 
     } 
    } 
    //gets trailer vrag classification. 
    int countTrailerVrag = 0; 
    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++) 
    { 
     if (mycurrentTrailerAvailableRoute[l] != null) 
     { 
      using (SqlCommand select = new SqlCommand("Select [TR_Classification] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "' AND [TR_Allocation] = " + 0, con)) 
      { 
       using (SqlDataReader readerS = select.ExecuteReader()) 
       { 
         while (readerS.Read()) 
         { 
          trailerVragAllocation = readerS.GetString(0); 
          myAvailableTrailerVragClassification[countTrailerVrag] = trailerVragAllocation; 
          countTrailerVrag++; 
         } 
         readerS.Close(); 
       } 
      } 
     } 
    } 

    int countTrailers = 0; 
    string trailerRegNum = null; 
    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++) 
    { 
     if (mycurrentTrailerAvailableRoute[l] != null) 
     { 
      using (SqlCommand selectT = new SqlCommand("Select [TR_RegNumber] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "'" + " AND [TR_Classification] = '" + myAvailableTrailerVragClassification[l] + "' AND [TR_Allocation] = " + 0, con)) 
      { 
       SqlDataReader readerT = selectT.ExecuteReader(); 
        if (readerT.HasRows) 
        { 
         while (readerT.Read()) 
         { 
          trailerRegNum = readerT.GetString(0); 
          myAvailableCurrentTraillerRegNumber[countTrailers] = trailerRegNum; 
          countTrailers++; 
         } 
        } 
        else 
        { 
         forloopBreak = false; 
        } 
       readerT.Close(); 
      } 
     } 
    }//END OF TRAILER CHECKING 

    //gets trailer's max tonnage 
    int myTrailerTonMax = 0; 
    int myTrailerTon = 0; 

    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++) 
    { 
     if (mycurrentTrailerAvailableRoute[l] != null) 
     { 
      using (SqlCommand selectT = new SqlCommand("Select [TR_MaxTonnage] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "'" + " AND [TR_Classification] = '" + myAvailableTrailerVragClassification[l] + "'", con)) 
      { 
       using (SqlDataReader readerS = selectT.ExecuteReader()) 
       { 
        while (readerS.Read()) 
        { 
         myTrailerTon = readerS.GetInt32(0); 
         myTrailerAvailableTonMax[myTrailerTonMax] = myTrailerTon; 
         myTrailerTonMax++; 
        } 
        readerS.Close(); 
       } 
      } 
     } 
    } 
} 

特典: - 我的數據庫中的數據與條件匹配,while循環甚至執行,但最終我的布爾值返回false。

+0

您的代碼太大,我們無法調查,您不能只發布相關位? –

+0

我會盡力去做 –

回答

0

如果最初在閱讀器中有任何數據,則readerT.HasRows子句將始終爲真。這裏是酒店的描述從MSDN page

獲取一個值,指示SqlDataReader是否包含一個或多個行。

你想做的事,而不是(我認爲)設置的內容forloopBreak = false;while循環結束。所以在這個使用塊裏面你把這個放在這裏:

SqlDataReader readerT = selectT.ExecuteReader(); 
while (readerT.Read()) 
{ 
    trailerRegNum = readerT.GetString(0); 
    myAvailableCurrentTraillerRegNumber[countTrailers] = trailerRegNum; 
    countTrailers++; 
} 
forloopBreak = false; 
readerT.Close(); 

讓我知道這是否可行!

+0

你是對的!這是它看起來的問題,謝謝! –

相關問題