Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Update

VB
Dim TotalBuild As Integer = ObjCls.GrdColTotal(GrdTower, 2) - DS.Tables(0).Rows(0)("NoOfBuilding")
                  Dim TotalElv As Integer = ObjCls.GrdColTotal(GrdTower, 4) - DS.Tables(0).Rows(0)("NoOfElevator")
                  Dim TotalEsc As Integer = ObjCls.GrdColTotal(GrdTower, 5) - DS.Tables(0).Rows(0)("NoOfEscalator")
                  Dim TotalTrav As Integer = ObjCls.GrdColTotal(GrdTower, 6) - DS.Tables(0).Rows(0)("NoOfTravelator")

                  If Val(TxtNoOfBuilding.Text) = 0 Then
                      lblTowerStatus.Text = "Not Allow Zero"
                  End If
                  If Val(TxtNoOfBuildingGrp.Text) = 0 Then
                      lblTowerStatus.Text = "Not Allow Zero"
                  End If
                  ' If TotalBuild + Val(TxtNoOfBuildingGrp.Text) > Val(TxtNoOfBuilding.Text) Then
                  If TotalBuild > Val(TxtNoOfBuilding.Text) Then
                      lblTowerStatus.Text = "Can't exceed Total Number Of Building"
                  ElseIf TotalElv + Val(TxtBulNoofEle.Text) > Val(TxtTotalElevator.Text) Then
                      lblTowerStatus.Text = "Total Number of Elevator exceeded"
                  ElseIf TotalEsc + Val(TxtBulNoofEscalator.Text) > Val(TxtTotalEscalator.Text) Then
                      lblTowerStatus.Text = "Total Number of Escalator exceeded"
                  ElseIf TotalTrav + Val(TxtBulNoOfTravelator.Text) > Val(TxtTotalTravelator.Text) Then
                      lblTowerStatus.Text = "Total Number of Travellator exceeded"
                  ElseIf Val(TxtNoOfBuilding.Text) = Val(TotalBuild) + Val(TxtNoOfBuildingGrp.Text) And ((Val(TxtTotalElevator.Text) <> Val(TotalElv) + Val(TxtBulNoofEle.Text)) Or (Val(TxtTotalEscalator.Text) <> Val(TotalEsc) + Val(TxtBulNoofEscalator.Text)) Or (Val(TxtTotalTravelator.Text) <> Val(TotalTrav) + Val(TxtBulNoOfTravelator.Text))) Then
                      lblTowerStatus.Text = "Total Number of Elevator not utilized"
                  ElseIf Val(TxtNoOfBuilding.Text) <> Val(TotalBuild) + Val(TxtNoOfBuildingGrp.Text) And ((Val(TxtTotalElevator.Text) = Val(TotalElv) + Val(TxtBulNoofEle.Text)) And (Val(TxtTotalEscalator.Text) = Val(TotalEsc) + Val(TxtBulNoofEscalator.Text)) And (Val(TxtTotalTravelator.Text) = Val(TotalTrav) + Val(TxtBulNoOfTravelator.Text))) Then
                      lblTowerStatus.Text = "Total Number of Building not utilized"
Posted
Comments
Christian Graus 20-Jun-13 2:18am    
This looks at first glance like it should work. What is your question ? It's ugly, but it should work. We're sure not going to try to decode it to find out what you want help with

1 solution

As Christian Graus says "It's ugly" and almost impossible to determine what, if anything is wrong with the logic.

Try the following steps to make this easier to read and for you to debug ...

1. TotalBuild, TotalElv, TotalEsc, TotalTrav are already Dim As Integer so there is no need to use Val(TotalBuild), Val(TotalElv), Val(TotalEsc), Val(TotalTrav) !! Get rid of the Val() surrounds.

2. I personally strongly dislike code that either disappears off to the right of the screen so I have to keep scrolling, or wraps around when pasted into CP. To address this consider doing all of the Val(txtTextBoxName.Text) stuff outside of your validation section. E.g.
VB
'Get values from input textBoxes
Dim NoOfBuilding As Integer = Val(TxtNoOfBuilding.Text)
Dim NoOfBuildingGrp As Integer = Val(TxtNoOfBuildingGrp.Text)
Dim TotalElevator As Integer = Val(TxtTotalElevator.Text)
Dim TotalTravelator As Integer = Val(TxtTotalTravelator.Text)
Dim BulNoofEscalator As Integer = Val(TxtBulNoofEscalator.Text)
Dim BulNoofEle As Integer = Val(TxtBulNoofEle.Text)
Dim TotalEscalator As Integer = Val(TxtTotalEscalator.Text)
Dim BulNoOfTravelator As Integer = Val(TxtBulNoOfTravelator.Text)
Then replace all the remaining instances of Val(TxtNoOfBuilding.Text) with NoOfBuilding etc

3. I also like to break down interim "calculations" - it means I can very quickly and easily check them during debugging without having to highlight partial lines of code (yes! I am a bit lazy like that!), so I would pull out some more stuff like this ...
VB
'Interim calculations
Dim tElev As Integer = TotalElv + BulNoofEle        'Total of all elevator types
Dim tEsc As Integer = TotalEsc + BulNoofEscalator   'Total of all escalator types
Dim tTrav As Integer = TotalTrav + BulNoOfTravelator    'Total of all Travelator types
Dim tBuild As Integer = TotalBuild + NoOfBuildingGrp    'Total of all Building types
'Have all forms of elevator been used?
Dim bElevators As Boolean = (TotalElevator = tElev And TotalEscalator = tEsc And TotalTravelator = tTrav)


4. Your two tests could be combined as they produce the same results. But more importantly the following tests could overwrite anything that you have already put into lblTowerStatus. For example if you enter 0 for the number of buildings and also enter too many Elevators then the error message will read "Total number of elevator exceeded" instead of "Not allow zero". There are a couple of ways to address this... you could use ElseIf or a helper function similar to this
VB
Private Sub Helper(ByVal text As String)
    'If you only want one error reported at a time in lblTowerStatus then do this
    If lblTowerStatus.Text.Length = 0 Then
        lblTowerStatus.Text = text
    End If
    'Or if you want to build up a list of the errors then use this instead
    'lblTowerStatus.Text += text
End Sub
This also has the advantage that if you change the way you report errors you only have to change one line of code.

So your tests finally become
VB
If NoOfBuilding = 0 Or NoOfBuildingGrp = 0 Then
    Helper("Not Allow Zero")
ElseIf TotalBuild > NoOfBuilding Then
    Helper("Can't exceed Total Number Of Building")
ElseIf tElev > TotalElevator Then
    Helper("Total Number of Elevator exceeded")
ElseIf tEsc > TotalEscalator Then
    Helper("Total Number of Escalator exceeded")
ElseIf tTrav > TotalTravelator Then
    Helper("Total Number of Travellator exceeded")
ElseIf (NoOfBuilding = tBuild And bElevators = False) Or (NoOfBuilding <> tBuild And bElevators = True) Then
    Helper("Total Number of Elevator not utilized")
ElseIf NoOfBuilding <> tBuild And bElevators = True Then
    Helper("Total Number of Building not utilized")
End If


NB I tested for bElevators = True and bElevators = False for clarity. You should really just use If bElevators ... Then or If Not bElevators ...Then

It should now be easier to see if the logic is what you actually intended and certainly easier to debug
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900