By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,045 Members | 1,305 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

# if textbox value is negative then increment 1

P: 14
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:

Expand|Select|Wrap|Line Numbers
1.
2. Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
3. Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
4. Dim ts As TimeSpan = dt1 - dt2
5. If Convert.ToInt32(ts.Days) <> 0 Then
6.     txtTtlDelay.Text = Convert.ToInt32(ts.Days)
7. ElseIf Convert.ToInt32(ts.Days) = 0 Then
8.     txtTtlDelay.Text = Convert.ToInt32(ts.Days)
9. Else
10.     Response.Write("Invalid Input")
11. End If
12.
can someone tell me where did i get wrong in my coding?
Attached Images invalid.jpg (19.9 KB, 54 views)
May 23 '15 #1

#### ✓ answered by computerfox

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

Try this:
Expand|Select|Wrap|Line Numbers
1. Dim dt1 As DateTime = Convert.ToDateTime(Date.Now)
2. Dim dt2 As DateTime = Convert.ToDateTime(txtDate2.Text)
3. Dim ts As TimeSpan = dt1 - dt2
4. If Convert.ToInt32(ts.Days) < 0 Then
5.     txtTtlDelay.Text = (Convert.ToInt32(ts.Days)-1)
6. Elseff Convert.ToInt32(ts.Days) > 0 Then
7.     txtTtlDelay.Text = (Convert.ToInt32(ts.Days))
8. ElseIf Convert.ToInt32(ts.Days) == 0 Then
9.     txtTtlDelay.Text = (Convert.ToInt32(ts.Days))
10. Else
11.     Response.Write("Invalid Input")
12. End If
13.

Share this Question
6 Replies

 100+ P: 278 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: Expand|Select|Wrap|Line Numbers 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. May 23 '15 #2

P: 14
Hello @ computerfox, the answer is still incorrect.
please see the attach picture.
Attached Images still incorrect.jpg (47.6 KB, 51 views)
May 23 '15 #3

 100+ P: 278 As mentioned, if the answer is -3 then that is decrement and you gave incorrect information. Try this: Expand|Select|Wrap|Line Numbers 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   May 23 '15 #4

 P: 14 ok, i knew it. i just change the add value into negative. So, I got the correct answer. Expand|Select|Wrap|Line Numbers   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 May 23 '15 #5

 100+ P: 278 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. May 23 '15 #6

 P: 14 @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 =) May 23 '15 #7

### Post your reply

Sign in to post your reply or Sign up for a free account. 