可以为null的数据类型 [英] Nullable datatypes

查看:60
本文介绍了可以为null的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




几天前我发布了关于模拟的可能性。在

..NET1.1 .NET2.0中可用的可以为空的数据类型 - 我差不多了,但是

需要更多的指导。


基本上,我有一个类文件,其中包含各种类定义,

如下:


使用System;


命名空间共享

{

公共类NullableDateTime

{

private日期时间pdtmValue;

公共NullableDateTime(DateTime的dtmValue){this.pdtmValue =

dtmValue;}

公共DateTime值{{返回这一点。 pdtmValue;}}

public bool HasValue {get {return this!= null;}}

};

}

然后我有另一个带有NullableDateTime属性的类文件,如下所示:


使用共享;

使用系统;

使用System.Collections;

使用System.Data;

使用System.Data.SqlClient;


命名空间DataFeeds

{

公共类CT ransaction

{

private NullableDateTime pTradeDate;

public NullableDateTime TradeDate

{

得到{return pTradeDate;}

set {pTradeDate = value;}

}

}

}

最后,我创建了一个CTransactions类的实例,如下所示:

CTransaction objTransaction = new CTransaction();


但是,除非我明确填充objTransaction.TradeDate,否则它是

undefined。


我希望objTransaction.TradeDate默认填充空值

及其HasValue属性返回false,除非我明确填充它,

在这种情况下我希望它的HasValue属性返回true,否则我可能

也不费心......


感激不尽的任何帮助。


Mark

Hi,

I posted a couple of days ago about the possibility of "simulating" in
..NET1.1 the nullable datatypes available in .NET2.0 - I''m nearly there, but
require a bit more guidance.

Basically, I have a class file which contains the various class definitions,
as follows:

using System;

namespace shared
{
public class NullableDateTime
{
private DateTime pdtmValue;
public NullableDateTime(DateTime dtmValue) {this.pdtmValue =
dtmValue;}
public DateTime value {get{return this.pdtmValue;}}
public bool HasValue {get{return this != null;}}
};
}
Then I have another class file with a NullableDateTime property, as follows:

using shared;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DataFeeds
{
public class CTransaction
{
private NullableDateTime pTradeDate;
public NullableDateTime TradeDate
{
get {return pTradeDate;}
set {pTradeDate = value;}
}
}
}
Finally, I create an instance of the CTransactions class, as follows:

CTransaction objTransaction = new CTransaction();

However, unless I explicitly populate objTransaction.TradeDate, it is
undefined.

I want objTransaction.TradeDate to be populated with a null value by default
and its HasValue property to return false unless I explicitly populate it,
in which case I want its HasValue property to return true, otherwise I may
as well not bother...

Any assistance gratefully received.

Mark

推荐答案

在你的NullableDateTime类中,这一行:
In your NullableDateTime class, this line:
public bool HasValue {get {return this!= null;}}
public bool HasValue {get{return this != null;}}



将始终为假。


如果你想使用类来为可空性包装值类型,你可以这样做:b / b
class NullableDateTime
{

public NullableDateTime(DateTime dt){this.Value = dt; }

public readonly DateTime Value;

};


那么NullableDateTime类型的给定变量可能为null,也可能不为null,但是

不需要HasValue功能。


如果你想使用结构来包装它们你可以一次使用Sql *

类型,例如使用可空性概念的SqlDateTime,或者如果你自己编写
我会建议模仿.net 2.0中的功能 - 即

如果Value属性为null,则抛出异常,并保留一个私有bool

成员以确定该对象是否具有值。

" Mark Rae写道:



几天前我发布了关于模拟的可能性。在
.. NET1.1中.NET2.0中可用的可以为空的数据类型 - 我差不多了,但是需要更多的指导。

基本上,我有包含各种类定义的类文件,如下所示:

使用系统;

命名空间共享
公共类NullableDateTime

DateTime的pdtmValue;
公共NullableDateTime(DateTime的dtmValue){this.pdtmValue =
dtmValue;}
公共DateTime值{{返回this.pdtmValue;公共bool HasValue {get {return this!= null;}}
};
}

然后我有另一个带有NullableDateTime属性的类文件,如下:

使用System;
使用System.Collections;
使用System.Data;
使用System.Data.SqlClient;

命名空间DataFeeds
{
公共类CTransaction
{
私有NullableDateTime pTradeDate; {
获取{return pTradeDate;}
设置{pTradeDate = value;}
}
}
}

最后,我创建了一个CTransactions类的实例,如下所示:

CTransaction objTransaction = new CTransaction();

但是,除非我明确填充objTransaction。 TradeDate,它是未定义的。

我希望objTransaction.TradeDate默认填充空值
并且其HasValue属性返回false,除非我明确填充它,
在这种情况下我希望它的HasValue属性返回true,否则我可能也不会打扰......

感激不尽的任何帮助。

Mark


will always ba false.

If you want to use classes to wrap value types for nullability you can just
do this:

class NullableDateTime
{
public NullableDateTime(DateTime dt) { this.Value = dt; }
public readonly DateTime Value;
};

Then a given variable of type NullableDateTime may or may not be null, but
there is no need for the HasValue functionality.

If you want to use structs to wrap them you could for one use the Sql*
types, such as SqlDateTime which uses the concept of nullability, or if you
write your own I would suggest mimicing the functionality in .net 2.0 -- i.e.
the Value property throws an exception if it''s null, and keep a private bool
member to determine whether the object has a value or not.
"Mark Rae" wrote:
Hi,

I posted a couple of days ago about the possibility of "simulating" in
..NET1.1 the nullable datatypes available in .NET2.0 - I''m nearly there, but
require a bit more guidance.

Basically, I have a class file which contains the various class definitions,
as follows:

using System;

namespace shared
{
public class NullableDateTime
{
private DateTime pdtmValue;
public NullableDateTime(DateTime dtmValue) {this.pdtmValue =
dtmValue;}
public DateTime value {get{return this.pdtmValue;}}
public bool HasValue {get{return this != null;}}
};
}
Then I have another class file with a NullableDateTime property, as follows:

using shared;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DataFeeds
{
public class CTransaction
{
private NullableDateTime pTradeDate;
public NullableDateTime TradeDate
{
get {return pTradeDate;}
set {pTradeDate = value;}
}
}
}
Finally, I create an instance of the CTransactions class, as follows:

CTransaction objTransaction = new CTransaction();

However, unless I explicitly populate objTransaction.TradeDate, it is
undefined.

I want objTransaction.TradeDate to be populated with a null value by default
and its HasValue property to return false unless I explicitly populate it,
in which case I want its HasValue property to return true, otherwise I may
as well not bother...

Any assistance gratefully received.

Mark



" Mark Rae" <毫安** @ mark-N-O-S-P-A-M-rae.co.uk>写在

新闻:e1 ************** @ TK2MSFTNGP15.phx.gbl:
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in
news:e1**************@TK2MSFTNGP15.phx.gbl:
我想要填充objTransaction.TradeDate使用默认的null值和它的HasValue属性返回false,除非我明确地填充它,在这种情况下我希望它的HasValue属性返回
true,否则我不用打扰...
I want objTransaction.TradeDate to be populated with a null value by
default and its HasValue property to return false unless I explicitly
populate it, in which case I want its HasValue property to return
true, otherwise I may as well not bother...




看看这个项目:
http://www.codeproject.com/useritems/CSharpSQL.asp

一开始迁移不明显,但是如果你看看它的源代码,你会注意到它做了我认为你想要的更多东西和它的DbValue类型。

-

Chad Z. Hower(又名Kudzu) - http://www.hower。 org / Kudzu /

编程是一种反击的艺术形式

使用IntraWeb授权ASP.NET
http:// www。 atozed.com/IntraWeb/



Take a look at this project:
http://www.codeproject.com/useritems/CSharpSQL.asp

It migth not be obvious at first, but if you look at its source code you will notice its doing what I think
you are asking for and more with its DbValue types.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/


" KH" < KH@discussions.microsoft.com>在消息中写道

新闻:C1 ********************************** @ microsof t.com ...
"KH" <KH@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
在你的NullableDateTime类中,这一行:
In your NullableDateTime class, this line:
public bool HasValue { get {return this!= null;}}
public bool HasValue {get{return this != null;}}


总是假的。


will always ba false.




实际上,它总是如此......

如果你想使用类来为可空性包装值类型,你可以这样做:

类NullableDateTime
{<公共NullableDateTime(DateTime dt){this.Value = dt; }
public readonly DateTime Value;
};

然后NullableDateTime类型的给定变量可能为null,也可能不为null,


优秀!谢谢。

但是不需要HasValue功能。



Actually, it''s always true...
If you want to use classes to wrap value types for nullability you can
just
do this:

class NullableDateTime
{
public NullableDateTime(DateTime dt) { this.Value = dt; }
public readonly DateTime Value;
};

Then a given variable of type NullableDateTime may or may not be null,
Excellent! Thanks.
but there is no need for the HasValue functionality.




但它可以模仿它,对吧......?



But it would be possible to mimic it, right...?


这篇关于可以为null的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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