Can I call a message from a TryParse in an if and statement?

Hekzdaddy 121 Reputation points
2021-04-26T00:31:52.383+00:00

Hello all, I am working on a project. I have a TryParse statement that reads:

If Not Integer.TryParse(TextBox1.Text, intPin(0)) Then

        MsgBox("Enter a number between 7 and 9.", MsgBoxStyle.OkOnly, "PIN1")
        Return False
    End If

I have the same statement checking values in different txtBoxes. Then I have a For...Loop statement checking for the min and max values. I have msgBox that incase the For...Loop with the IF statement is false. Instead of msgBox("check your pin"). Would I be able to return the message from the TryParse >>MsgBox("Enter a number between 7 and 9.", MsgBoxStyle.OkOnly, "PIN1") . The message changes for each textbox.

Dim X As Boolean
For intCount As Integer = 0 To (intPin.Length - 1)

        If intPin(intCount) >= intMinPin(intCount) AndAlso
             intPin(intCount) <= intMaxPin(intCount) Then
        Else
            X = True
        End If

    Next

    If X Then
        MsgBox("Check your pin")

    Else
        MsgBox("Pin is correct")
    End If

Thanks in advance.

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2021-04-26T05:17:22.673+00:00

    If Not Integer.TryParse(Val(TextBox1.Text), intPin(0)) _
    AndAlso Val(TextBox1.Text) >= intMinPin(0) _
    AndAlso Val(TextBox1.Text) <= intMaxPin(0) Then
    MsgBox("Enter a number between 7 and 9.", MsgBoxStyle.OkOnly, "PIN1")
    Return False
    End If

    Be very careful when using negations (Not) in compound
    conditionals. Two things to watch out for:

    (1) The "Not" applies to the immediately following
    condition, but doesn't apply to the condition after
    the conjunction/disjunction. The "Not" must also
    appear there if what is desired is to test for both
    conditions being untrue. Your compound conditional
    is asking: "If the TryParse fails AND if the number
    is within the valid range ..."

    (2) When using tests for negation (Not) you usually
    have to use a disjunction (OrElse), not a conjunction
    (AndAlso). When testing for true conditions you usually
    are asking "If condition1 is true AND if condition2 is
    true then do x". But when testing for negations you usually
    are asking "If condition1 is NOT true OR if condition2 is
    NOT true then do x".

    For example:

    If Not Integer.TryParse(Val(TextBox1.Text), intPin(0)) _
      OrElse Not Val(TextBox1.Text) >= intMinPin(0) _
      OrElse Not Val(TextBox1.Text) <= intMaxPin(0) Then
        MsgBox("Enter a number between 7 and 9.", MsgBoxStyle.OkOnly, "PIN1")
        Return False
    End If
    
    • Wayne

1 additional answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-04-26T02:55:33.823+00:00

    Hi @Hekzdaddy ,
    Create a panel and put all needed textboxes into the panel, then you can validate these textboxes in panel.
    The code looks like:

            For Each txtBox In Panel1.Controls.OfType(Of TextBox)()  
                If Not intPin(intCount) >= intMinPin(intCount) AndAlso  
                  intPin(intCount) <= intMaxPin(intCount) Then  
      
                    MessageBox.Show($"{txtBox.Name}'s pin is incorrect,check your pin")  
                End If  
            Next  
    

    Hope it could be helpful.
    Besides, if I have any misunderstanding, please provide more details here.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.