question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked WayneAKing-0228 commented

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

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.



dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered Hekzdaddy-3952 edited

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

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That makes total sense and that a great explanation. Thank you so much.

0 Votes 0 ·

So in the future, it would be best to omit the "not", and thanks for the info on the OrElse, thats great info I will forever remember.

0 Votes 0 ·

So in the future, it would be best to omit the "not",

It's usually less confusing for most people (IMO) if we
use positive expressions rather than negative ones. I
have seen numerous cases of programmers using a
conjunction instead of a disjunction with negative
tests in compound conditionals, which then failed
to give the desired/expected results.

Of course, there may be situations where negative
tests are more convenient or readable than positive
tests. So flexibility is needed.

You should also remain open to using multiple conditional
(If) statements instead of a single compound conditional
if it adds clarity and/or simplicity to the tests. Some
programmers mistakenly believe they are writing more
efficient code by compacting and connecting statements
together whenever they can. In most cases that isn't
true, as the compiler(s) will often create the same
object code from multiple/nested If statements as it
will from a single compound If.

  • Wayne

0 Votes 0 ·

Got it, again thanks for sharing your knowledge. I really enjoy learning the language and I hope to get a good grip on it. After VB I intend on taking a course on others like C++ or Java, hopefully there are some similar concepts as there is a lot to learn.

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered WayneAKing-0228 commented

Hi @Hekzdaddy-3952 ,
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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That sounds like it would work, I can try it. I am also trying this:

Private Function ValidateInputFields() As Boolean

     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

But, it appears that the TryParse is just checking for the integer and the program moves on, it does not check for the equation.

0 Votes 0 ·

Do you mean that Integer.TryParse(Val(TextBox1.Text), …) is always True? It seems inappropriate to use such constructs.


0 Votes 0 ·

Hello Viorel, it was just moving on to the next TryParse, but when I implemented the orElse statements, it checked for all values to be where they should be (i.e between intMinPin and intMaxPin), if anything failed, message would result. Otherwise, it would move on to next txtBox.

0 Votes 0 ·

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

A follow-up observation about your TryParse:

You should not have Val(TextBox1.Text) there. That converts
the string from TextBox1.Text to a Double. The value in that
Double must then be implicitly converted back to a String
to pass as an argument to TryParse. If you had Option Strict
enabled you would get an error from that implicit conversion.

You should be passing the string from TextBox1 directly
to TryParse:

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

  • Wayne

0 Votes 0 ·