比较Timespan值 [英] Compare Timespan Values

查看:118
本文介绍了比较Timespan值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何:

公共TimeSpan超时

{

get {return timeout; }

设定

{

timeout = value;

if(timeout< licenseTimeout)

licenseTimeout =超时;

}

}

私人TimeSpan超时= TimeSpan.FromMinutes(10);


在VB.NET中转换为VB.NET Opertor''<''没有为类型定义

''Sysem.TimeSpan''和System.TimeSpan''

TIA


Russ

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

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

Russ

推荐答案

你可以使用CompareTo方法TimeSpan这个''将比较两个TimeSpans


" Russ Green"写道:
You can use the CompareTo method of TimeSpan this ''will compare two TimeSpans

"Russ Green" wrote:
这是怎么回事:
公共TimeSpan Timeout
{
get {return timeout; }
设置
{timeout / value;
if(timeout< licenseTimeout)
licenseTimeout = timeout;
}
}
private TimeSpan timeout = TimeSpan.FromMinutes(10);

在VB.NET中转换为VB.NET Opertor''<''未定义类型
''Sysem .TimeSpan''和System.TimeSpan''

TIA

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

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

Russ



Russ,


让自己轻松一点,你可以在日期时间和时间范围内永远比较

刻度。


但是,不是通过例如日期时间戳设定日期,而不是你必须以另一种方式做到这一点。


但是你的情况我认为这是要走的路。


我希望这会有所帮助,


Cor
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


Russ,

而不是具体询问如何比较两个TimeSpan值我通常

请问如何比较任何两个va .NET中的lues。


.NET中可比的值覆盖Object.Equals for

identity等于或实现IComparable接口。

覆盖Object.Equals或实现IComparable的类通常也会覆盖

=,<>,<,< =,>和> =运营商。不幸的是VB.NET 2002& 2003年没有

直接支持重载运算符,而你需要明确调用

例程(在=运算符的情况下,例程是op_Equality,参见

运算符重载使用指南如下所示.VS.NET 2005(又名

Whidbey,将于2005年晚些时候发布, http://lab.msdn.microsoft.com/vs2005/

支持使用和定义重载运营商。


因此,对于TimeSpan,我可以简单地调用TimeSpan.Compare或

TimeSpan.Compare来比较TimeSpan值。


使用Equals,Compare或CompareTo允许我的代码在所有类型中保持一致,而不是需要记住我需要什么属性或者
需要用于每种类型。例如,如果我有一个Person类型,我可以简单地将
调用Person.Compare,而不是需要记住什么属性集
$ b类是Person类的b(属性)。当然比较

属性/属性集的危险是代码重复,通过调用Person.Compare

比较本身被巧妙地封装在Person对象中。此外,

比较例程是多态的,因为每种类型都有自己的比较...对于VS.NET 2005我们将会重载运算符,因此我可以简单

使用重载的运算符...


我不会依赖比较Ticks或其他属性,因为恕我直言是

奇数球解决方案,奇数球解决方案,奇数球解决方案。是你有两个或更多

相似的结构(比较对象)&你做两个或更多不同的

方式(DateTime.Ticks,Person.Name)。 Oddball Solution是一种代码气味

,在重构为模式中标识
http://www.industriallogic.com/xp/refactoring/


几个月前进行了长时间的讨论在这一组中使用=

运算符或标题为比较空值类型的Equals方法来自

约2004年11月17日。请参阅以下开头的整个帖子:

http://groups-beta.google.com/group/ .. .52a3ff291c3f36

有关常见超载操作员的列表&替代方法见

运算符重载使用指南 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


希望这有助于

Jay


" Russ Green" <毫安**** @ SPAMrussgreen.com>在留言中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

|这是怎么回事:

|公共TimeSpan超时

| {

|得到{return timeout; }

|设置

| {

|超时=值;

| if(timeout< licenseTimeout)

| licenseTimeout = timeout;

| }

| }

|私人TimeSpan超时= TimeSpan.FromMinutes(10);

|

|在VB.NET中转换为VB.NET Opertor''<''没有为类型定义

| ''Sysem.TimeSpan''和System.TimeSpan''

|

|

| TIA

|

| Russ

|

|
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.Compare or
TimeSpan.CompareTo 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 "Refactoring 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****@SPAMrussgreen.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| How does this:
| public TimeSpan Timeout
| {
| get { return timeout; }
| set
| {
| timeout = value;
| if(timeout < licenseTimeout)
| licenseTimeout = timeout;
| }
| }
| private TimeSpan timeout = TimeSpan.FromMinutes(10);
|
| Convert to VB.NET when in VB.NET Opertor ''<'' is not defined for types
| ''Sysem.TimeSpan'' and System.TimeSpan''
|
|
| TIA
|
| Russ
|
|


这篇关于比较Timespan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆