Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 21st, 2006, 11:56 AM
Authorized User
 
Join Date: Jun 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default null values for properties

Hi There,
I was wondering if there are some best practices for handling null value assigments for properties.

For instance, how would I get to following property to accept a null value returned from the database, so I can assign the property value after I instantiated the class?

    Public Property Date_Start() As DateTime Implements Task._Date_Start
        Get
            Return _Date_Start
        End Get
        Set(ByVal value As DateTime)
            _Date_Start = value
        End Set
    End Property

'below gets an error in the class method when retrieving the instance
_Date_Start = prmDate_Start.Value


Thanks for your help.

Paul Olofson

 
Old April 23rd, 2006, 05:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

.NET 2 has introduced the concepts of a nullable type, allowing you to assign null to a DateTime.

http://blogs.msdn.com/vbteam/archive...28/143813.aspx
http://msdn.microsoft.com/vcsharp/20...nullabletypes/

Alternatively, and that's how I often did it in .NET 1.x applications, you can sign it a sensible "nullable in your opinion" default value, like DateTime.MinValue.

Then you can compare against MinValue and it the date is still MinValue you can assume it's null. However, the Nullable type is of course much cleaner.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: The Carnival Is Over by Dead Can Dance (Track 14 from the album: Dead Can Dance 1981-1998 (Limited Edition) [CD2]) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null values jmcgranahan BOOK: Access 2003 VBA Programmer's Reference 0 August 29th, 2006 02:25 PM
Handling Null Values nvenkat75 ADO.NET 2 January 16th, 2006 05:46 PM
Checking for Null Values asmodeus BOOK: Beginning VB.NET Databases 2 June 3rd, 2005 03:18 PM
How to Replace Null Values??? Varg_88 Classic ASP Basics 5 October 7th, 2004 02:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.