locked
A problem when using Call function RRS feed

  • Question

  • Hi all, I have the following code:

    Public Class Form1
    
      Dim sBookingID As String = ""
      Dim sBookingCost As String = ""
      Dim bError As Boolean = False
    
      Private Sub CheckBookingID()
        sBookingID = ""
        If txtBookingID.Text = "" Then
          sBookingID = "- You cannot have empty BookingID" & vbCrLf
          bError = True
        End If
      End Sub
    
      Private Sub CheckCost()
        sBookingCost = ""
        If txtBookingCost.Text = "" Then
          sBookingCost = "- You cannot have empty Booking Cost" & vbCrLf
          bError = True
        End If
      End Sub
    
      Private Sub btnAddBooking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBooking.Click
        Call CheckBookingID()
        Call CheckCost()
    
        If bError = True Then
          MsgBox(sBookingID & sBookingCost)
        Else
          lbxBookingID.Items.Add(txtBookingID.Text)
          lbxBookingCost.Items.Add(FormatCurrency(txtBookingCost.Text))
          txtBookingID.Clear()
          txtBookingCost.Clear()
        End If
      End Sub
    End Class

     

    My problem in here is If I add inputs which are not satisfied my conditions, it normally shows message box. However, when I fix it and input something, it still shows me a message box, but this time a message box contains nothing (If I combine them into one subroutine, it works fine). I have to stop and run it again to avoid this problem. Can anyone give my a suggestion in here, please?

    Cheers.

     

    Wednesday, April 13, 2011 2:19 PM

Answers

  • Here's another solution that might fix it.  The basic problem is that oce you detect an error, you set the flag, bError, to True, you never reset to false anywhere.

     

      If bError = True Then
       MsgBox(sBookingID & sBookingCost)
       bError = False
      Else
    

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by Min Zhu Monday, April 25, 2011 1:55 AM
    Wednesday, April 13, 2011 3:43 PM
  • You need bError=False right before the first CheckBookingID call in your Click Event.  This way you always start with a "clean slate."

    Because it's scoped at the Class level, it doesn't unset or clear when other subs exit.


    It never hurts to try. In a worst case scenario, you'll learn from it.
    • Proposed as answer by Rudedog2 Wednesday, April 13, 2011 3:43 PM
    • Marked as answer by Min Zhu Monday, April 25, 2011 1:54 AM
    Wednesday, April 13, 2011 2:34 PM
  • Another option is to use Functions to return true or false instead of using subroutines and setting a class level variable to true/false.

    Public Class Form1
    
      Private Function ValidateBookingID() As Boolean
    
        Return txtBookingID.Text.Length > 0
    
      End Function
    
      Private Function ValidateCost() As Boolean
    
        Return txtBookingCost.Text.Length > 0
    
      End Function
    
      Private Sub btnAddBooking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBooking.Click
    
        If Not ValidateBookingID() Then
          MessageBox.Show("You cannot have empty BookingID")
          txtBookingID.Focus()
        ElseIf Not ValidateCost() Then
          MessageBox.Show("You cannot have empty Booking Cost")
          txtBookingCost.Focus()
        Else
          lbxBookingID.Items.Add(txtBookingID.Text)
          lbxBookingCost.Items.Add(FormatCurrency(txtBookingCost.Text))
          txtBookingID.Clear()
          txtBookingCost.Clear()
        End If
    
      End Sub
    End Class
    

    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
    • Marked as answer by Min Zhu Monday, April 25, 2011 1:55 AM
    Wednesday, April 13, 2011 4:47 PM

All replies

  • You need bError=False right before the first CheckBookingID call in your Click Event.  This way you always start with a "clean slate."

    Because it's scoped at the Class level, it doesn't unset or clear when other subs exit.


    It never hurts to try. In a worst case scenario, you'll learn from it.
    • Proposed as answer by Rudedog2 Wednesday, April 13, 2011 3:43 PM
    • Marked as answer by Min Zhu Monday, April 25, 2011 1:54 AM
    Wednesday, April 13, 2011 2:34 PM
  • Here's another solution that might fix it.  The basic problem is that oce you detect an error, you set the flag, bError, to True, you never reset to false anywhere.

     

      If bError = True Then
       MsgBox(sBookingID & sBookingCost)
       bError = False
      Else
    

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by Min Zhu Monday, April 25, 2011 1:55 AM
    Wednesday, April 13, 2011 3:43 PM
  • I was trying both of your methods, and I think these are the same, but I don't know why it still doesn't work.

    Wednesday, April 13, 2011 4:41 PM
  • Another option is to use Functions to return true or false instead of using subroutines and setting a class level variable to true/false.

    Public Class Form1
    
      Private Function ValidateBookingID() As Boolean
    
        Return txtBookingID.Text.Length > 0
    
      End Function
    
      Private Function ValidateCost() As Boolean
    
        Return txtBookingCost.Text.Length > 0
    
      End Function
    
      Private Sub btnAddBooking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBooking.Click
    
        If Not ValidateBookingID() Then
          MessageBox.Show("You cannot have empty BookingID")
          txtBookingID.Focus()
        ElseIf Not ValidateCost() Then
          MessageBox.Show("You cannot have empty Booking Cost")
          txtBookingCost.Focus()
        Else
          lbxBookingID.Items.Add(txtBookingID.Text)
          lbxBookingCost.Items.Add(FormatCurrency(txtBookingCost.Text))
          txtBookingID.Clear()
          txtBookingCost.Clear()
        End If
    
      End Sub
    End Class
    

    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
    • Marked as answer by Min Zhu Monday, April 25, 2011 1:55 AM
    Wednesday, April 13, 2011 4:47 PM
  • I think Matt's Object Oriented approach is far superior to using a flag.  Which should also make it inherently faster because you are not reading/writing a variable. 

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Wednesday, April 13, 2011 7:11 PM