我布尔值,即使所有的代码是正确的假

问题描述:

关于我的计划:我布尔值,即使所有的代码是正确的假

我的算法(这类)是为了检查交付是否已完成,事后提供的用于卡车/拖车/驱动器另一种交货,同时该算法发送另一辆货车/拖车/司机。概括起来这个类执行以下操作:

  • 检查预约是否在分配模式(分配模式基本上意味着“预订”是在被交付
  • 检查进度是否有吨位左预订(例如,我想向一个地方发送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

我会尽力去做 –

如果最初在阅读器中有任何数据,则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

你是对的!这是它看起来的问题,谢谢! –