Nullable datatypes

M

Mark Rae

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
 
G

Guest

In your NullableDateTime class, this line:

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.
 
C

Chad Z. Hower aka Kudzu

Mark Rae said:
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...

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/
 
M

Mark Rae

In your NullableDateTime class, this line:


will always ba false.

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...?
 
J

Jay B. Harlow [MVP - Outlook]

Mark,
In addition to the other comments:

Make NullableDateTime a struct instead of a class. Add a "hasValue" field in
addition to the HasValue property. Set hasValue to True in the constructor.
Consider what needs to happen in the Value property when hasValue is False,
for example throw an exception maybe...

Something like:

| public struct NullableDateTime
| {
| private readonly DateTime pdtmValue;

private readonly bool hasValue;

| public NullableDateTime(DateTime dtmValue)
{
this.pdtmValue = dtmValue;
this.hasValue = true;
}
| public DateTime Value {get{return this.pdtmValue;}}
| public bool HasValue {get{return hashValue;}}
| };

When you create a NullableDateTime without the constructor, Value will equal
DateTime.MinValue & HasValue will be false.

When you create a NullableDateTime with the constructor, Value will have the
value passed & HasValue will be true.

As the others suggest look at SqlDateTime as it already does the above, plus
overloads some operators so it plays nice with DateTime itself...

Hope this helps
Jay


| 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
|
|
 
M

Mark Rae

Make NullableDateTime a struct instead of a class. Add a "hasValue" field
in
addition to the HasValue property. Set hasValue to True in the
constructor.

Sounds good.
Consider what needs to happen in the Value property when hasValue is
False,
for example throw an exception maybe...

Having the HasValue property ought to get round that, since I will not call
the Value property unless HasValue is true.
When you create a NullableDateTime without the constructor, Value will
equal
DateTime.MinValue & HasValue will be false.

When you create a NullableDateTime with the constructor, Value will have
the
value passed & HasValue will be true.

And will this work when instantiating a class which has a NullableDateTime
property? I.e. the property will be null until / unless I give it a
value...?
As the others suggest look at SqlDateTime as it already does the above,
plus
overloads some operators so it plays nice with DateTime itself...

Yes, but I want to do this for the other non-nullable datatypes as well...
 
J

Jay B. Harlow [MVP - Outlook]

Mark,
| And will this work when instantiating a class which has a NullableDateTime
| property? I.e. the property will be null until / unless I give it a
| value...?
Yes.

| > As the others suggest look at SqlDateTime as it already does the above,
| > plus
| > overloads some operators so it plays nice with DateTime itself...
|
| Yes, but I want to do this for the other non-nullable datatypes as well...

The System.Data.SqlTypes (where SqlDateTime is) contains most of the common
non-nullable datatypes.

http://msdn.microsoft.com/library/d.../en-us/cpref/html/frlrfSystemDataSqlTypes.asp

To make any non-nullable datatype .NET 2.0 & Generics will make it easier,
in .NET 1.0 & 1.1 I would consider making a utility that used the CodeDom to
generate the "Nullable" structure specific to each non-nullable datatype for
me. The code the utility created would be similar to what is in
System.Data.SqlTypes (same sort of conversion operators & other
operators)...

Hope this helps
Jay


| |
| > Make NullableDateTime a struct instead of a class. Add a "hasValue"
field
| > in
| > addition to the HasValue property. Set hasValue to True in the
| > constructor.
|
| Sounds good.
|
| > Consider what needs to happen in the Value property when hasValue is
| > False,
| > for example throw an exception maybe...
|
| Having the HasValue property ought to get round that, since I will not
call
| the Value property unless HasValue is true.
|
| > When you create a NullableDateTime without the constructor, Value will
| > equal
| > DateTime.MinValue & HasValue will be false.
| >
| > When you create a NullableDateTime with the constructor, Value will have
| > the
| > value passed & HasValue will be true.
|
| And will this work when instantiating a class which has a NullableDateTime
| property? I.e. the property will be null until / unless I give it a
| value...?
|
| > As the others suggest look at SqlDateTime as it already does the above,
| > plus
| > overloads some operators so it plays nice with DateTime itself...
|
| Yes, but I want to do this for the other non-nullable datatypes as well...
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top