473,594 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a null value to a DateTime property

Hi,

I have a class with several properties, one of which is of the DateTime
datatype. However, this property will not always have a value, and I need to
check later whether it has a value or not.

When I create a new instance of the class, this property acquires a default
value of {1/1/1}, which I don't want at all, as that is a perfectly valid
date.

Is there any way to cause a DateTime datatype to have no value at all, which
I can check later?

Or is the answer not to use a DataTime datatype and use a generic object
datatype instead?

Any assistance gratefully received.

Mark
Nov 17 '05 #1
11 16413
Dates are value types, not reference types, so they can not be null, they
hold always a value. Could you use DateTime.MinVal ue as a special value to
denote no value?

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> escribió en el mensaje
news:ev******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

I have a class with several properties, one of which is of the DateTime
datatype. However, this property will not always have a value, and I need
to check later whether it has a value or not.

When I create a new instance of the class, this property acquires a
default value of {1/1/1}, which I don't want at all, as that is a
perfectly valid date.

Is there any way to cause a DateTime datatype to have no value at all,
which I can check later?

Or is the answer not to use a DataTime datatype and use a generic object
datatype instead?

Any assistance gratefully received.

Mark

Nov 17 '05 #2
Mark,

In .NET 2.0, you would use the Nullable<T>, with the type parameter T
equal to DateTime. This will produce a structure which has a HasValue
property which is true if there is a value, false if it is null.

If you are using a version of .NET before 2.0, then you could use the
SqlDateTime, which supports null semantics.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ev******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

I have a class with several properties, one of which is of the DateTime
datatype. However, this property will not always have a value, and I need
to check later whether it has a value or not.

When I create a new instance of the class, this property acquires a
default value of {1/1/1}, which I don't want at all, as that is a
perfectly valid date.

Is there any way to cause a DateTime datatype to have no value at all,
which I can check later?

Or is the answer not to use a DataTime datatype and use a generic object
datatype instead?

Any assistance gratefully received.

Mark

Nov 17 '05 #3
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAM sogecable.com> wrote in
message news:OK******** ******@tk2msftn gp13.phx.gbl...
Could you use DateTime.MinVal ue as a special value to denote no value?


Possibly - thanks for the answer.
Nov 17 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uM******** *****@TK2MSFTNG P15.phx.gbl...
In .NET 2.0, you would use the Nullable<T>, with the type parameter T
equal to DateTime. This will produce a structure which has a HasValue
property which is true if there is a value, false if it is null.
Still using v1.1
If you are using a version of .NET before 2.0, then you could use the
SqlDateTime, which supports null semantics.


I'll try that - thanks.
Nov 17 '05 #5
KH
You could also write your own class containing a DateTime that would then be
nullable. This might be easier than having to reference System.Data to use
SqlDateTime.

class MyDateTime
{
public MyDateTime(Date Time dt) { this._dt = dt; }

private DateTime _dt;

public DateTime Value { get { return this._dt; } }
};

"Mark Rae" wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uM******** *****@TK2MSFTNG P15.phx.gbl...
In .NET 2.0, you would use the Nullable<T>, with the type parameter T
equal to DateTime. This will produce a structure which has a HasValue
property which is true if there is a value, false if it is null.


Still using v1.1
If you are using a version of .NET before 2.0, then you could use the
SqlDateTime, which supports null semantics.


I'll try that - thanks.

Nov 17 '05 #6
"KH" <KH@discussions .microsoft.com> wrote in message
news:FC******** *************** ***********@mic rosoft.com...
You could also write your own class containing a DateTime that would then
be
nullable. This might be easier than having to reference System.Data to use
SqlDateTime.

class MyDateTime
{
public MyDateTime(Date Time dt) { this._dt = dt; }

private DateTime _dt;

public DateTime Value { get { return this._dt; } }
};


Hmm - that looks rather good! Thanks very much :-)
Nov 17 '05 #7
Just for completeness, C# also allows declaring a nullable type like
this:

int? NullableInteger ;

The ? denotes a nullable type.

VB does not have a similar syntax.

Nov 17 '05 #8
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
Just for completeness, C# also allows declaring a nullable type like
this:

int? NullableInteger ;

The ? denotes a nullable type.

VB does not have a similar syntax.


Interesting - could I apply that here...?

public class CSecurityClassn
{
private DateTime pMaturityDate;
public DateTime MaturityDate
{
get {return pMaturityDate;}
set {pMaturityDate = value;}
}
}
Nov 17 '05 #9
This applies only to VC#.2005

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
421
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I am developing an application that utilizes a separate class library for data access to a SQL Server database. One of the tables has several fields defined as DateTime. The data access layer exposes properties for each field in the table. In the instance of datetime fields, the class returns a property of date type. The problem occurs when one of the datetime fields is NULL, which results in a...
9
548
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
4
2187
by: js | last post by:
I have a stored procedure named "processInventory" like the following. Depending on the passed in parameters, I would like to add a WHERE clause for "select" action. For example, if any varchar type of parameter is passed in, the where clause would use "LIKE" operator. For example, "Select * from Main where like @Serial. All other types will use "=" operator. For example, "Select * from Main where MAKE = @Make and Type = @type". How...
2
24613
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime in the xml.
7
2606
by: MP | last post by:
Hello, I am trying to write a class that will expose some properties. One of the property is extracted from a SQL database and can be NULL or some integer value. Is there a elegant way of implementing this in C# ? I do not want to use variant (or similar types) because I want to retain a strnong types property. Will I have no choices but to add another property that would set/get the NULL value?
6
6510
by: Markus Eßmayr | last post by:
Hello, I'd like to consume a WebService, which returns an array of objects which include several members of type System.String, System.Decimal and System.DateTime. In the WSDL-file, the members of the object are marked as nilable. I generated the client classes using VS.NET 2003. After the creation, I got the class-definition of the objects returned by the WebService too. BUT, only the System.String members where marked to be nullable,...
2
1870
by: Fabiano | last post by:
Please, i have an webform, that work with a object that receives a DateTime value. I don't want to save date into it. How can i verify if it's value is null or not? Tks Fabiano
5
1412
by: lextendo | last post by:
Hi all, I have a problem which seems it has to have a simple solution, though I can't figure it out and I can't find a solution on usenet either. Two forms: Form A contains a textbox where the user can enter a date. When doubleclicking the textbox, a form B wil open with a CalenderControl to pick a date. Now I want to copy this date -selected in form B- in the textbox of
0
2591
by: tschroeder250 | last post by:
Hi Everyone, There are so many Nullable DateTimePickers out there that this post may not even be found among all the others, but I wanted to try. First, I want to thank all the authors of the code I used as a starting point. Second, I hope this code helps someone as much as they helped me. I have built a Nullable DateTimePicker and NullableCalendarColumn/Cell/EditingControl that is nearly 100% complete, as far as I can tell. There is...
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8009
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8240
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.