if textbox value is negative then increment 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayakamacy
    New Member
    • May 2015
    • 14

    if textbox value is negative then increment 1

    how to construct conditional statement if the textbox value is negative then it will increament 1

    for example:

    the textbox is: -2
    then in database it will save by increamenting as 1.

    it will become as: -3

    can someone help me out for this problem. thanks
    ----
    i have here a sample code for computing delays of date.
    but then the computation if d2 is greater than d1 it will be a negative result. but the answer is not accurate (see the attach picture)

    here is my code:

    Code:
    Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
    Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
    Dim ts As TimeSpan = dt1 - dt2
    If Convert.ToInt32(ts.Days) <> 0 Then
        txtTtlDelay.Text = Convert.ToInt32(ts.Days)
    ElseIf Convert.ToInt32(ts.Days) = 0 Then
        txtTtlDelay.Text = Convert.ToInt32(ts.Days)
    Else
        Response.Write("Invalid Input")
    End If
    can someone tell me where did i get wrong in my coding?
    Attached Files
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    By the looks of it, your conditional statements don't do anything.
    The first set checks if it's greater or less than 0 and the second might not even be valid as it needs to be "==", but in both sets you do the same statement.

    Try this:

    Code:
    Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
    Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
    Dim ts As TimeSpan = dt1 - dt2
    If Convert.ToInt32(ts.Days) < 0 Then
        txtTtlDelay.Text = Convert.ToInt32(ts.Days)+1
    Elseff Convert.ToInt32(ts.Days) > 0 Then
        txtTtlDelay.Text = Convert.ToInt32(ts.Days)
    ElseIf Convert.ToInt32(ts.Days) == 0 Then
        txtTtlDelay.Text = Convert.ToInt32(ts.Days)
    Else
        Response.Write("Invalid Input")
    End If
    Hope that helps!

    For the record, "increment" means ++ so -2 would be -1.
    Decrement would cause it to be -3.

    Comment

    • ayakamacy
      New Member
      • May 2015
      • 14

      #3
      still incorrect

      Hello @ computerfox, the answer is still incorrect.
      please see the attach picture.
      Attached Files

      Comment

      • computerfox
        Contributor
        • Mar 2010
        • 276

        #4
        As mentioned, if the answer is -3 then that is decrement and you gave incorrect information.

        Try this:
        Code:
        Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
        Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
        Dim ts As TimeSpan = dt1 - dt2
        If Convert.ToInt32(ts.Days) < 0 Then
            txtTtlDelay.Text = (Convert.ToInt32(ts.Days)-1)
        Elseff Convert.ToInt32(ts.Days) > 0 Then
            txtTtlDelay.Text = (Convert.ToInt32(ts.Days))
        ElseIf Convert.ToInt32(ts.Days) == 0 Then
            txtTtlDelay.Text = (Convert.ToInt32(ts.Days))
        Else
            Response.Write("Invalid Input")
        End If

        Comment

        • ayakamacy
          New Member
          • May 2015
          • 14

          #5
          ok, i knew it. i just change the add value into negative.
          So, I got the correct answer.

          Code:
          Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
          Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
          Dim ts As TimeSpan = dt1 - dt2
          If Convert.ToInt32(ts.Days) < 0 Then
              txtTtlDelay.Text = Convert.ToInt32(ts.Days) + -1
          ElseIf Convert.ToInt32(ts.Days) > 0 Then
              txtTtlDelay.Text = Convert.ToInt32(ts.Days)
          ElseIf Convert.ToInt32(ts.Days) = 0 Then
               txtTtlDelay.Text = Convert.ToInt32(ts.Days)
          Else
               Response.Write("Invalid Input")
          End If
          anyways, thanks @computerfox

          Comment

          • computerfox
            Contributor
            • Mar 2010
            • 276

            #6
            Ehhh.... you should not do that, it's just not appealing.
            Try the new code I provided and also keep in mind for the future that parenthesis help.

            Comment

            • ayakamacy
              New Member
              • May 2015
              • 14

              #7
              @computerfox, i already used your new code.
              and the result now is accurate.
              yeah right, i will now use parenthesis so that it can be look like a formal coding. hehe, thanks anyway =)

              Comment

              Working...