question

HemanthB-9452 avatar image
0 Votes"
HemanthB-9452 asked HemanthB-9452 commented

Age Calculator C#

Hi, I recently made an age calculator using C# Win form application. So I set this "If" code for the application saying that if the Date of Birth is set greater than the Current date, an error message should pop up.

Code:

'if (dtpcurrentdate.Value < dtpdateofbirth.Value) ;
{


                 MessageBox.Show("Current date must be greater than Date of Birth. Please Try Again");
                    
             }'

When I run the application, test it and set the Date of Birth greater than the Current date, the error message is popping. But when I set Date of Birth lesser than the current date, the error message still pops up. My aim is that the error should not occur when users set the Date of Birth lesser than the current date and should only pop up if they set it greater than the current date. Please help!

dotnet-csharp
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 HemanthB-9452 commented

if (dtpcurrentdate.Value < dtpdateofbirth.Value);
{
MessageBox.Show("Current date must be greater ...");
}

If the above is what you actually have in your program,
then the reason why the MessageBox always appears is
because you have a semicolon at the end of the if
statement line. Remove it.

The semicolon ends the scope of the if statement so
the code in the block between the braces {...} is
not part of the conditional and so will always be
executed.

  • Wayne

· 1
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.

Thanks so much for your help! It really did work.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered HemanthB-9452 commented

Remove the first ';'.

· 1
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.

Thanks so much for your help! It really did work.

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

My guess is that you may have misplaced the two DateTimePickers.

Try to output the values of two DateTimePickers instead of prompt statements.


If the response 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.

· 2
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.

Misplaced? You mean like the names of the DateTimePickers?

How should I output the values?

0 Votes 0 ·

Yes, I guess it is possible to put dtpcurrentdate in the position of dtpdateofbirth.
As for the output, we just want to see their values, so either MessageBox.show() or Console.WriteLine() can be used.

   MessageBox.Show(dtpdateofbirth.Value+" || "+dtpcurrentdate.Value);
0 Votes 0 ·