views:

56

answers:

2

Hi Guys,

Please can any one advise me if it is possible to decalre a custom structure that can be assigned Nothing and / or DbNull.Values, and also can be instanciated as Nothing?

What I am looking to do is ceate a custom DateTime object that can recieve a DBNull.Value from a database query and also start life as Nothing. IS this possible?

Thanks in advance.

Best regards, Duane

+2  A: 

Seems like Nullable<DateTime> (DateTime? for short, or Date? in VB.NET) gets you almost all the way there. You just need to specially deal with the conversion to/from DBNull on your own.

// You can set a DateTime? to null.
DateTime? d = null;

// You can also set it to a DateTime.
d = DateTime.Now;

// You can check whether it's null in one of two ways:
if (d == null || !d.HasValue) // (These mean the same thing.)
{ }

// Boxing a DateTime? will either result in null or a DateTime value.
SetDatabaseValue(d);

// As for conversions from DBNull, you'll have to deal with that yourself:
object value = GetDatabaseValue();
d = value is DBNull ? null : (DateTime?)value;
Dan Tao
Hi Dan,Thanks for your reply, great solution but I probably should have made myself a little clearer with what I am looking for. What I want to do is create a new custom DateTime object that can accept either a valid system DateTime, a DBull.value or can be set to Nothing (we are using VB, but am familiar with c#syntax). I did not really want to convert all of the current DateTime declarations in the project to Nullable(of DateTime) (or DateTime?) and then change all the code where these objects are accessed to look for the .HasValue property.
Dib
Continued..There is simply too much code to change (We are converting a massive VB6 project to .Net 3.5). What I want to do is create a single custom structure that handles all checking and boxing intenally so we can just swap out the System.DateTime structure with our custom DateTime structure.I hope that helps explain the plan a bit better!Regards,Duane.
Dib
@Dib: So, one very important question is: do you want this to be a *`Class`* or a *`Structure`*? This becomes a particularly tricky situation in VB.NET, because you can take a `Structure` and set it to `Nothing` *but it won't really be `Nothing`*; it'll just have the default value (e.g., 0) for that type. That said, if you can answer me this I think I can help you out a bit more.
Dan Tao
Hi Dan, Ideally I would prefer a structure, mainly because System.DateTime is also a structure, but the default value issue would not be desirable unless it could equal DBNull.Value. So I guess a class maybe the only way to go. That would simplify things as any class can be set to Nothing. I'd still need to work on it being able to be assigned the DBNull.Value and I am not sure how it would work when setting a sqlcommand parameter value, and such like. Does that help at all?<br/>Thanks in advance. Regards Dib.
Dib
A: 

If you're working with DataSets, use the Field and SetField DataRow extension methods. These allow you to use Nullable types without worrying about DBNull anymore.

E.g., suppose the "MyDateField" field (of type DateTime) is nullable. Then you can do something like this:

foreach (var row in myDataTable)
{
    // will return null if the field is DbNull
    var currentValue = row.Field<DateTime?>("MyDateField");

    // will set the value to DbNull.Value
    row.SetField<DateTime?>("MyDateField", null);
}
jeroenh
Hi jeroenh, Thanks for your reply. I have not seen SetField method used before; will look into that.
Dib