473,763 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare Timespan Values

How does this:
public TimeSpan Timeout
{
get { return timeout; }
set
{
timeout = value;
if(timeout < licenseTimeout)
licenseTimeout = timeout;
}
}
private TimeSpan timeout = TimeSpan.FromMi nutes(10);

Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
'Sysem.TimeSpan ' and System.TimeSpan '
TIA

Russ
Nov 17 '05 #1
11 9726
You can use the CompareTo method of TimeSpan this 'will compare two TimeSpans

"Russ Green" wrote:
How does this:
public TimeSpan Timeout
{
get { return timeout; }
set
{
timeout = value;
if(timeout < licenseTimeout)
licenseTimeout = timeout;
}
}
private TimeSpan timeout = TimeSpan.FromMi nutes(10);

Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
'Sysem.TimeSpan ' and System.TimeSpan '
TIA

Russ

Nov 17 '05 #2
Russ,

Make it yourself easy, you can in datetime and timespan forever compare the
ticks.

However, not when it is a by by instance a by a datetimepicker set date,
than you have to do it in another way.

But in your case I think that it is the way to go.

I hope this helps,

Cor
Nov 17 '05 #3
Russ,
Rather then ask specifically how to Compare two TimeSpan values I normally
ask how to Compare any two values in .NET.

Values that are comparable in .NET either override Object.Equals for
identity Equals or implement the IComparable interface. Classes that
override Object.Equals or Implement IComparable also normally override the
=, <>, <, <=, >, and >= operators. Unfortunately VB.NET 2002 & 2003 do not
directly support overloaded operators, instead you need to explicitly call
the routine (in the case of = operator the routine is op_Equality, see
"Operator Overloading Usage Guidelines" listed below. VS.NET 2005 (aka
Whidbey, due out later in 2005, http://lab.msdn.microsoft.com/vs2005/) will
support using & defining overloaded operators.

So in the case of TimeSpan I can simply call TimeSpan.Compar e or
TimeSpan.Compar eTo to compare TimeSpan Values.

Using Equals, Compare, or CompareTo allows my code to be consistent across
all types, rather then needing to remember what property or properties I
need use for each type. For example, if I had a Person type I can simply
call Person.Compare, rather then needing to remember what set of attributes
(properties) of the Person class. Of course the danger of comparing sets of
attributes/properties is code duplication, by calling Person.Compare the
comparison itself is neatly encapsulated in the Person object. Further the
Compare routine is polymorphic, in that each type has its own Compare... Of
course with VS.NET 2005 we will have overloaded operators so I can simply
use the overloaded operators...

I would not rely on comparing Ticks or other properties as that IMHO is an
"oddball solution", an "Oddball Solution" is when you have two or more
similar constructs (comparing objects) & you do it two or more different
ways (DateTime.Ticks , Person.Name). "Oddball Solution" is a "code smell"
that is identified in "Refactorin g to Patterns"
http://www.industriallogic.com/xp/refactoring/.

There was a long discussion a few months back in this group on using =
operators or the Equals method titled "Comparing Empty Value Types" from
about November 17, 2004. See the entire thread starting with:

http://groups-beta.google.com/group/...52a3ff291c3f36
For a list of commonly overloaded Operators & the alternative methods see
"Operator Overloading Usage Guidelines" at:
http://msdn.microsoft.com/library/de...Guidelines.asp

IComparable:
http://msdn.microsoft.com/library/de...ClassTopic.asp

Object.Equals:
http://msdn.microsoft.com/library/de...ualsTopic1.asp

http://msdn.microsoft.com/library/de...lsOperator.asp

Hope this helps
Jay

"Russ Green" <ma****@SPAMrus sgreen.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
| How does this:
| public TimeSpan Timeout
| {
| get { return timeout; }
| set
| {
| timeout = value;
| if(timeout < licenseTimeout)
| licenseTimeout = timeout;
| }
| }
| private TimeSpan timeout = TimeSpan.FromMi nutes(10);
|
| Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
| 'Sysem.TimeSpan ' and System.TimeSpan '
|
|
| TIA
|
| Russ
|
|
Nov 17 '05 #4
Jay B. Harlow [MVP - Outlook] <Ja************ @msn.com> wrote:
Using Equals, Compare, or CompareTo allows my code to be consistent across
all types, rather then needing to remember what property or properties I
need use for each type.


Does that mean you use them for Int32 (etc) as well? If not, where do
you draw the line?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Jay B. Harlow [MVP - Outlook] <Ja************ @msn.com> wrote:
Using Equals, Compare, or CompareTo allows my code to be consistent across
all types, rather then needing to remember what property or properties I
need use for each type.


Does that mean you use them for Int32 (etc) as well? If not, where do
you draw the line?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Jon,
Although I don't state it, I thought it was easily inferred:

First choice is the = operator on the values themselves (not properties of
the values). Seeing as VB.NET supports the = operator on both Int32 &
DateTime, I use the = operator.

Unfortunately VB.NET 2002 & 2003 does not support overloaded operators, so I
"fall back" to the Equals method, rather then risk duplicate code by using
one or more properties of the values. Of course in VB.NET 2005 overloaded
operators are supported, ergo I will use the overloaded = operator. Unless I
am working on a polymorphic routine where Object.Equals, IEquatable(Of T),
IComparable, or IComparable(Of T) make more sense, then I would use one of
these Interfaces or Object.Equals instead...

Seeing as C# does support overloading the = operator, in C# I would favor
the = operator...

The point I am attempting to make is that IMHO its better to compare the
values/objects themselves, rather then rely on comparing
attributes/properties of the said values/objects.

Hope this helps
Jay

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
| Jay B. Harlow [MVP - Outlook] <Ja************ @msn.com> wrote:
| > Using Equals, Compare, or CompareTo allows my code to be consistent
across
| > all types, rather then needing to remember what property or properties I
| > need use for each type.
|
| Does that mean you use them for Int32 (etc) as well? If not, where do
| you draw the line?
|
| --
| Jon Skeet - <sk***@pobox.co m>
| http://www.pobox.com/~skeet
| If replying to the group, please do not mail me too
Nov 17 '05 #7
Doh!

Of course types that don't actually override Object.Equals nor implement one
of IEquatable(Of T), IComparable, or IComparable(Of T), nor overload any of
the comparison operators then of course, as a last resort, I would compare
specific attributes/properties. Especially is it was a type I could not
modify... Of course I would consider encapsulating this comparison in a
Comparer object (Singleton?) to minimize duplicate code...

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uk******** ******@TK2MSFTN GP15.phx.gbl...
| Jon,
| Although I don't state it, I thought it was easily inferred:
|
| First choice is the = operator on the values themselves (not properties of
| the values). Seeing as VB.NET supports the = operator on both Int32 &
| DateTime, I use the = operator.
|
| Unfortunately VB.NET 2002 & 2003 does not support overloaded operators, so
I
| "fall back" to the Equals method, rather then risk duplicate code by using
| one or more properties of the values. Of course in VB.NET 2005 overloaded
| operators are supported, ergo I will use the overloaded = operator. Unless
I
| am working on a polymorphic routine where Object.Equals, IEquatable(Of T),
| IComparable, or IComparable(Of T) make more sense, then I would use one of
| these Interfaces or Object.Equals instead...
|
| Seeing as C# does support overloading the = operator, in C# I would favor
| the = operator...
|
| The point I am attempting to make is that IMHO its better to compare the
| values/objects themselves, rather then rely on comparing
| attributes/properties of the said values/objects.
|
| Hope this helps
| Jay
|
| "Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
| news:MP******** *************** *@msnews.micros oft.com...
|| Jay B. Harlow [MVP - Outlook] <Ja************ @msn.com> wrote:
|| > Using Equals, Compare, or CompareTo allows my code to be consistent
| across
|| > all types, rather then needing to remember what property or properties
I
|| > need use for each type.
||
|| Does that mean you use them for Int32 (etc) as well? If not, where do
|| you draw the line?
||
|| --
|| Jon Skeet - <sk***@pobox.co m>
|| http://www.pobox.com/~skeet
|| If replying to the group, please do not mail me too
|
|
Nov 17 '05 #8
Jay B. Harlow [MVP - Outlook] <Ja************ @msn.com> wrote:
Although I don't state it, I thought it was easily inferred:

First choice is the = operator on the values themselves (not properties of
the values). Seeing as VB.NET supports the = operator on both Int32 &
DateTime, I use the = operator.
But doesn't that then go against:

<quote>
Using Equals, Compare, or CompareTo allows my code to be consistent
across all types, rather then needing to remember what property or
properties I need use for each type.
</quote>

I could understand a pattern which said "for all the primitive types,
use operators, but for all other types use Equals" but to use the
operator for DateTime goes against that. If it's okay to remember
DateTime, might there not be other value types it's worth remembering?
The point I am attempting to make is that IMHO its better to compare the
values/objects themselves, rather then rely on comparing
attributes/properties of the said values/objects.


Sure.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Jay,

In my opinin is this the same discussion as the toArgb discussion.

Although in this case is the difference in the problem, that your for the
colours very valid point, that you want be more clear in the code, about
what colours you are talking, is less important. You only want to know if
date "A" was before or after date "B" or that the time difference between
"A" and "B" was equal, higher or less from each other.

Just my opinion.

Cor
Nov 17 '05 #10

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

Similar topics

2
21096
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an agent talks about 5 hours per day, this comes out to about 150 hours of talk time per month. Fine. But, the DateTime and TimeSpan format strings will only output hours from 0-23. I need something that will output time as such: 150:0:0 to...
3
13553
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
10
4222
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
12
61221
by: conckrish | last post by:
Hi all.. Can anyone tell me how to compare datetime objects?I ve three objects namely Current date,start date and end date.. I need to check the current date with Start date and end date....Plz tell me how to compare ??? Thanx.. Krish
3
28507
by: Mark | last post by:
I'd like to compare two datetime values in milliseconds. The datetime.compare method appears to show only seconds. Milliseconds of a datetime are available as a property of each datetime, but I am assuming there is an easier way than comparing the milliseconds, seconds, minutes, hours, days, etc, individually to generate the difference. Thanks in advance. Mark
1
4115
by: Will | last post by:
This little puzzle has been bugging me for some time. I'm trying to create a little service that will check a time frame and compare it to another timeframe (start / stop time). A real-world example is as follows: Patsy would like to volunteer time at a booth this year at the fair. Her optimal timeframe to volunteer is between 1:00pm and 5:00pm and she inputs this via some form. Let's say that I want to compare this to a...
1
1383
by: =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post by:
II want to compare how many seconds there are between files. If the files are within a 1 - 10 second range I want to copy them to their own folders. What I have so far is a couple methods that take in all the files in a directory / subdirectories and sort them by their lastWriteTime stamp: Dim flist As List(Of System.IO.FileInfo) Private Sub writeDirectories(ByVal ParentPath As String) For Each sDirectory As String In...
3
15980
by: Curious | last post by:
I have code below. But it won't compile. Error: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime' DateTime now = DateTime.Now.TimeOfDay; DateTime openTime = Convert.ToDateTime("9:30:00"); if (now < openTime)
25
4546
by: Brian | last post by:
I have a datetimepicker formated for just time, the user selects the time. I want to compare if that time is between midnight and 8 am dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM# this evaluates to true when the time is not greater than dtmTime #9:32:34 PM# Why is that? and How can I get this to work? Brian
0
9386
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,...
1
9937
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,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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
5270
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.