Question : Using IsDate In VB.Net 2005

I am taking a require beginners VB class and i can't seem to get my project working.  I am trying to set up an if statement so that if the quantity is zero the reorder date entered is a valid date >= current date.  and if the quantity on hand is not zero, reorder date must not be entered.

My code is below..i am not sure if what i have so far is correct, help appreciated thank you!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
 
        If txtDescription.Text = "" Then
            MessageBox.Show("Description is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtDescription.Focus()
            Exit Sub
        End If
        If IsNumeric(txtQuantity.Text) = False Then
            MessageBox.Show("Quantity on hand must be an integer value >= zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtQuantity.Focus()
            Exit Sub
        End If
        If Integer.Parse(txtQuantity.Text) < 0 Then
            MessageBox.Show("Quantity on hand must be an integer value >= zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtQuantity.Focus()
            Exit Sub
        End If
        If txtQuantity.Text = 0 And _
            txtReorderDate.Text = "" = False Then
            MessageBox.Show("Reorder date not used when quantity <> zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtQuantity.Focus()
            Exit Sub
        End If

Answer : Using IsDate In VB.Net 2005

Hello aspirinmornings,

All of the Is... functions simply return a boolean value (True/False) indicating whether or not the given argument is ... well ... whatever that function is checking for. So, for example, IsDate, merely checks whether or not the argument (txtReorderDate.Text) is a date. If your professor said that IsDate(txtReorderDate.Text) < Now would work, I am afraid your prof. is mistaken (Boolean on the left side of the equation + Date on the right side = Not a good combination).

Having said that, using IsDate is valuable precisely to avoid the (Conversion from string "" to type 'Date' is not valid.) error message. First, use IsDate to make sure txtReOrderDate.Text is indeed a date, and if so, THEN compare it to Now (as JackOfPH indicated). e.g.
  If IsDate(txtReOrderDate.Text) AndAlso CDate(txtReOrderDate.Text) < Now Then
    ...
  End If

BTW, in case you didn't know AndAlso is simply a shortcut way to say "Evaluate second expression only if first expression is true". You could also put the second expression  in a nested If (which indeed may be more appropriate if you need to handle and Else condition as well). e.g.
  If IsDate(txtReOrderDate.Text) Then
    If CDate(txtReOrderDate.Text) < Now Then
      ...
    Else
      ...
    End If
  End If

While on the topic of evaluating expressions:
1) txtReorderDate.Text = "" = False
Technically, this works, (because of operator precedence) but is a bit confusing to read and is just setting you up for logical errors down the road. txtReorderDate.Text <> "" is a more typical way of doing it.
2) IsDate(txtReorderDate.Text) = False
I'm getting a bit nitpicky here (and admittedly this a bit of a style choice), but generally I like to avoid using "= False" or "= True", because that means that what is on the left side of that equation is already a boolean value (what your are trying to get down to in an If...Then expression) in the first place. So if you want to check if something is True, leave it as is, and if you want to check if something is False, precede it by a Not. e.g.
  Not IsDate(txtReorderDate.Text)

Regards,

cavehop
Random Solutions  
 
programming4us programming4us