473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

What am I missing about comparing Dates here?

Hi all,

The following function is the ValueChanged handler for two
DateTimePicker thingies - allowing the user to specify a date range. I
don't understand why the following bug exists: when the first picker is
set later than the second, the messagebox pops up *twice* in
succession.

What's causing that? How can I achieve the (obvious) desired effect?

Thanks for any thoughts,

cdj
================
private void checkDates(ref DateTimePicker first, DateTimePicker
second)
{
if (first.Value.Date second.Value.Date)
{
MessageBox.Show("End date may not be earlier than start date.",
"Rates Aggregator");

//I would MUCH rather not allow the change to be made at all,
//but this will do for now.
first.Value = second.Value;
}
}

private void dateTimePickerStart_ValueChanged(object sender,
System.EventArgs e)
{
checkDates(ref dateTimePickerStart, dateTimePickerEnd);
}

Nov 6 '06 #1
8 1674
VJ
You are changing the value again in valuechanged event, so it will fire
twice. Scary part is if you put the assignment outside the if (first.Value =
second.Value;
), it is a infinite loop.

VJ

"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
Hi all,

The following function is the ValueChanged handler for two
DateTimePicker thingies - allowing the user to specify a date range. I
don't understand why the following bug exists: when the first picker is
set later than the second, the messagebox pops up *twice* in
succession.

What's causing that? How can I achieve the (obvious) desired effect?

Thanks for any thoughts,

cdj
================
private void checkDates(ref DateTimePicker first, DateTimePicker
second)
{
if (first.Value.Date second.Value.Date)
{
MessageBox.Show("End date may not be earlier than start date.",
"Rates Aggregator");

//I would MUCH rather not allow the change to be made at all,
//but this will do for now.
first.Value = second.Value;
}
}

private void dateTimePickerStart_ValueChanged(object sender,
System.EventArgs e)
{
checkDates(ref dateTimePickerStart, dateTimePickerEnd);
}

Nov 6 '06 #2

VJ wrote:
You are changing the value again in valuechanged event, so it will fire
twice.
(smacks forehead) Thanks!

Maybe it's just late in the day, but I'm not seeing offhand how to
achieve the effect I'm after. Perhaps a local counter variable in the
handler ("how many times have I been here?") would do the trick.

Is there some standard way to limit date entries in the way I'm after?
Thanks again for the logic lesson! lol

cdj

Nov 6 '06 #3
VJ
anytime. Please never feel bad, it happens to everybody.. that is why people
are here to help!

I would inherit and write my own DateTime class and have method to write the
checks/validations that I need.

VJ

"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
>
VJ wrote:
>You are changing the value again in valuechanged event, so it will fire
twice.

(smacks forehead) Thanks!

Maybe it's just late in the day, but I'm not seeing offhand how to
achieve the effect I'm after. Perhaps a local counter variable in the
handler ("how many times have I been here?") would do the trick.

Is there some standard way to limit date entries in the way I'm after?
Thanks again for the logic lesson! lol

cdj

Nov 7 '06 #4
sherifffruitfly <sh*************@gmail.comwrote:
The following function is the ValueChanged handler for two
DateTimePicker thingies - allowing the user to specify a date range. I
don't understand why the following bug exists: when the first picker is
set later than the second, the messagebox pops up *twice* in
succession.

What's causing that? How can I achieve the (obvious) desired effect?
<snip>

I see your actual problem is fixed, but I'm concerned that you're
passing parameters by reference unnecessarily. That usually indicates a
lack of understanding of reference types or parameter passing.
Hopefully this link will help you:

http://www.pobox.com/~skeet/csharp/parameters.html

(Note that even though DateTime is a struct, DateTimePicker is a
class.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 7 '06 #5

Jon wrote:
I see your actual problem is fixed, but I'm concerned that you're
passing parameters by reference unnecessarily. That usually indicates a
lack of understanding of reference types or parameter passing.
Hopefully this link will help you:

http://www.pobox.com/~skeet/csharp/parameters.html

(Note that even though DateTime is a struct, DateTimePicker is a
class.)
Thanks Jon. I was doing that on purpose, actually. It occurred to me
that this was a place where it made sense (albeit trivial) to want to
actually change the input, rather than just computing with it. And
since I had never used ref/out variables before, I decided to go for
it.

In particular, using by-value variables, I would've had to specifically
*name* the control whose value I wanted to reset in the handler. I
thought this "coupling" was unneccesary, and if I passed by reference,
I could say "change the value of whatever the input was, where ever it
came from".

It's perfectly possible, of course, that I am suffering from numerous
confusions - thanks for the page!

cdj

Nov 7 '06 #6
sherifffruitfly <sh*************@gmail.comwrote:
http://www.pobox.com/~skeet/csharp/parameters.html

(Note that even though DateTime is a struct, DateTimePicker is a
class.)

Thanks Jon. I was doing that on purpose, actually. It occurred to me
that this was a place where it made sense (albeit trivial) to want to
actually change the input, rather than just computing with it. And
since I had never used ref/out variables before, I decided to go for
it.
But you're not using the fact that you're passing it by reference.
In particular, using by-value variables, I would've had to specifically
*name* the control whose value I wanted to reset in the handler.
No you wouldn't. You can still pass the reference by value, and then
change the displayed DateTime.
I thought this "coupling" was unneccesary, and if I passed by reference,
I could say "change the value of whatever the input was, where ever it
came from".
But you're not changing it for another instance of DateTimePicker -
you're just changing the *contents* of the DateTimePicker.
It's perfectly possible, of course, that I am suffering from numerous
confusions - thanks for the page!
I suggest you try just removing the "ref" from your code (in both
declaration and call) and observe how the code still works - then read
the page carefully to understand why!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 7 '06 #7
Any date you can walk away from is a good one.

--
;-),

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
sherifffruitfly <sh*************@gmail.comwrote:
http://www.pobox.com/~skeet/csharp/parameters.html
<snip--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 7 '06 #8

Jon wrote:
sherifffruitfly <sh*************@gmail.comwrote:
http://www.pobox.com/~skeet/csharp/parameters.html
>
(Note that even though DateTime is a struct, DateTimePicker is a
class.)
Thanks Jon. I was doing that on purpose, actually. It occurred to me
that this was a place where it made sense (albeit trivial) to want to
actually change the input, rather than just computing with it. And
since I had never used ref/out variables before, I decided to go for
it.

But you're not using the fact that you're passing it by reference.
I suggest you try just removing the "ref" from your code (in both
declaration and call)
Aw damn - you're right of course. Changing the object and changing the
value of a property of the object aren't the same thing. Sigh. And I
was so excited have found a "naturally occuring" pass-reference
sitution. :(

lol! Thanks for going slowly with me!

cdj

Nov 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike N. | last post by:
I am currently using a function to validate a form on the client side (see code below). At the end of the function, I would like it to also compare a startDate against an endDate to ensure that the...
2
by: Frank | last post by:
The DB in question contains daily stock transactions keyed by ticker (symbol) and date. Unfortunately all dates do not exist for all ticker symbols. To assist the process, there are two...
2
by: Duppypog | last post by:
I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database is 11/5/2003 with today being 11/6/2003. Can...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
6
by: Simon Harvey | last post by:
Hi everyone, I need to be able to compare to dates to ensure that one is at least 1 day greater than the other. Im trying to do if(toDate !> fromDate){ // handle }
4
by: Working_Girl | last post by:
Hi, I have a database with insurance clients and their dependents (spouses and children). We had a problem in the past with the twins and some of them have been entered with one month...
3
by: Alex Pavluck | last post by:
I have a date stored like this '2004 - 2006' and I use this code to get startyear and stopyear StartYear: Trim(Left(,InStr(,"-")-1)) StopYear: Trim(Right(,InStr(,"-")-1)) Is there a way to...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.