dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] swag

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    39

    Resolved [RESOLVED] swag

    What i say is followed by wether or not it works. [Yes = Works, No = Doesnt.]
    I'm trying to make it so that:
    If checkbox1 and checkbox2 are both checked, a msg box comes up. [yes]
    If textbox4 is empty a msg box comes up. [yes]
    If textbox5 is empty a msg box comes up. [yes]
    If neither checkbox1 or checkbox2 are checked, a msg comes up. [yes]
    If any of these errors are found, msg box comes up asking to fix errors. [no]

    My code:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ((CheckBox1.Checked) And (CheckBox2.Checked)) Then
                MsgBox("Error: How can you possibly be both the Money and Item Sender?", MsgBoxStyle.Critical)
            End If
            If TextBox4.Text = "" Then
                MsgBox("Error: Please include your Steam ID!", MsgBoxStyle.Critical)
            End If
            If TextBox5.Text = "" Then
                MsgBox("Error: Please include their Steam ID", MsgBoxStyle.Critical)
            End If
            If ((Not CheckBox1.Checked) And (Not CheckBox2.Checked)) Then
                MsgBox("Error: Please select a trade role.", MsgBoxStyle.Critical)
            End If
            If (Not ((CheckBox1.Checked) And (CheckBox2.Checked))) + (Not TextBox4.Text = "") + (Not TextBox5.Text = "") + (Not ((Not CheckBox1.Checked) And (Not CheckBox2.Checked))) Then
                Dim oForm As Form3
    
                oForm = New Form3()
    
                oForm.Show()
    
                oForm = Nothing
    
                Me.Hide()
            Else
                MsgBox("Please fix errors.", MsgBoxStyle.Information)
            End If
        End Sub

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,542

    Re: swag

    You might want to give your threads more meaningful names. I almost deleted this as spam without reading it.

    Anyway, you've got two general approaches to this. The approach you're currently using is to do one big final check for all the possible error states. To fix that you simply need to change the + symbols to ands. At the moment you're adding a bunch of Booleans together which doesn't really make sense.

    However, I personally think that's pretty unreadable and will be difficult to maintain. A more maintainable approach would be to maintain a separate Boolean to record whether or not any error has occurred. This would start out as false and you'd set it to true in each of your If blocks, e.g.:-
    Code:
            If ((CheckBox1.Checked) And (CheckBox2.Checked)) Then
                atLeastOneErrorOccurred = True
                MsgBox("Error: How can you possibly be both the Money and Item Sender?", MsgBoxStyle.Critical)
            End If
    Your final check to see whether to display the form or the "Please fix errors" message would then look like
    Code:
    If not atLeaseOneErrorOccurred then
    which is much more readable.

    Even better would be to break all the validation code out to its own method which either returned Boolean or threw an exception if the validation failed.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    39

    Re: swag

    Anyway, you've got two general approaches to this. The approach you're currently using is to do one big final check for all the possible error states. To fix that you simply need to change the + symbols to ands. At the moment you're adding a bunch of Booleans together which doesn't really make sense.
    Alright, I did this:
    Code:
    Public Class Form2
        Dim atLeaseOneErrorOccured As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ((CheckBox1.Checked) And (CheckBox2.Checked)) Then
                MsgBox("Error: How can you possibly be both the Money and Item Sender?", MsgBoxStyle.Critical)
            End If
            If TextBox4.Text = "" Then
                MsgBox("Error: Please include your Steam ID!", MsgBoxStyle.Critical)
            End If
            If TextBox5.Text = "" Then
                MsgBox("Error: Please include their Steam ID", MsgBoxStyle.Critical)
            End If
            If ((Not CheckBox1.Checked) And (Not CheckBox2.Checked)) Then
                MsgBox("Error: Please select a trade role.", MsgBoxStyle.Critical)
            End If
            If ((Not ((CheckBox1.Checked) And (CheckBox2.Checked)))) Then
                atLeaseOneErrorOccured = True
            End If
            If ((CheckBox1.Checked) And (CheckBox2.Checked)) Then
                atLeaseOneErrorOccured = True
            End If
            If (TextBox4.Text = "") Then
                atLeaseOneErrorOccured = True
            End If
            If (TextBox5.Text = "") Then
                atLeaseOneErrorOccured = True
            End If
            If atLeaseOneErrorOccured Then
                MsgBox("Fix your errors!", MsgBoxStyle.Information)
            End If
            If Not atLeaseOneErrorOccurred Then
                Dim oForm As Form3
    
                oForm = New Form3()
    
                oForm.Show()
    
                oForm = Nothing
    
                Me.Hide()
    
            End If
        End Sub
    The thing thats bolded [the "if not atLeastOneErrorOccur"] is saying its not declared.
    How do i fix this? Sorry if its obvious, im a noob to VB.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,826

    Re: swag

    Code:
    Dim atLeaseOneErrorOccured As String
    It should be a Boolean instead of a String

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    39

    Re: swag

    So just replace
    Code:
    Dim atLeaseOneErrorOccured as String
    with
    Code:
    Dim atLeaseOneErrorOccured as Boolean

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: swag

    Although that will get rid of the error, you still are not using that variable. Notice that FunkyDexter's solution uses that variable inside each validation block by setting the variable to True if any of those validation checks fails to pass.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width