473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Da te second.Value.Da te)
{
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 dateTimePickerS tart_ValueChang ed(object sender,
System.EventArg s e)
{
checkDates(ref dateTimePickerS tart, dateTimePickerE nd);
}

Nov 6 '06 #1
8 1688
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

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** *************@f 16g2000cwb.goog legroups.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.Da te second.Value.Da te)
{
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 dateTimePickerS tart_ValueChang ed(object sender,
System.EventArg s e)
{
checkDates(ref dateTimePickerS tart, dateTimePickerE nd);
}

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

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ h54g2000cwb.goo glegroups.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.comwrot e:
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.co m>
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.comwrot e:
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.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
sherifffruitfly <sh************ *@gmail.comwrot e:
http://www.pobox.com/~skeet/csharp/parameters.html
<snip--
Jon Skeet - <sk***@pobox.co m>
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.comwrot e:
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
8599
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 endDate is greater than (comes after) the startDate. The date format I'm using is MM/DD/YYYY and it's writing to an MS SQL Server 2000 database table via ASP. THANKS!!! function ValidateForm() { //validate STATUS element
2
1612
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 temporary tables: Tickers (the collection of all tickers) AND TransactionDates (the collection of all dates) The latest failed attempt at finding the missing transaction dates by
2
1839
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 someone spot the problem? Thanks, Lynnette Here's the code: sSQL = "Select PWExpire FROM tblUsers where strUserName = '" & stUser & "' AND strPassword = '" & hshPW & "'"
17
3037
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). I have a dataset as follows (1 week to keep it short): Employee 1 - Date 1 Employee 1 - Date 2
6
2294
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
2649
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 difference in their dates of birth, some of them have the same dates of births. I need to query both cases. In the case where they are set up with one month difference, the dates of birth has to be within 4 months of each other and excluding the ones...
3
2244
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 get this to only run on NON MISSING Project Dates? Thanks! Alex
2
1986
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. Thanks, Dante
4
2364
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 = CDate(MydateString) ''it is highlighted in this line.. txtActCode = Mydate i don't know what's wrong with this one..
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.