DateTime as a method parameter

  • Thread starter Abhishek Srivastava
  • Start date
A

Abhishek Srivastava

Hello All,

I am facing a simple problem. I have a method which accepts two
parameters. Both are of type DateTime.

However when calling this method there are times when the caller does
not have a value of DateTime with it. I tried calling the method with
null params but that didn't work because DateTime is a struct.

but I don't want to assign any values to it... because eventally the
call goes to a stored proc and the proc know what values to use when
the input is null.

What can I do to overcome this problem. Right now it appears to me
that I cannot pass a null to it ... but at the same time I don't want
to assign any values to these params in my code.

regards,
Abhishek.
 
J

Jürgen Beck

Abhishek Srivastava said:
I am facing a simple problem. I have a method which accepts two
parameters. Both are of type DateTime.

However when calling this method there are times when the caller does
not have a value of DateTime with it. I tried calling the method with
null params but that didn't work because DateTime is a struct.

but I don't want to assign any values to it... because eventally the
call goes to a stored proc and the proc know what values to use when
the input is null.

What can I do to overcome this problem. Right now it appears to me
that I cannot pass a null to it ... but at the same time I don't want
to assign any values to these params in my code.

Overload the method with only one DateTime-parameter can be a solution.

Jürgen Beck
MCSD.NET, MCDBA, MCT
www.Juergen-Beck.de
 
J

Jon Skeet [C# MVP]

Abhishek Srivastava said:
I am facing a simple problem. I have a method which accepts two
parameters. Both are of type DateTime.

However when calling this method there are times when the caller does
not have a value of DateTime with it. I tried calling the method with
null params but that didn't work because DateTime is a struct.

but I don't want to assign any values to it... because eventally the
call goes to a stored proc and the proc know what values to use when
the input is null.

What can I do to overcome this problem. Right now it appears to me
that I cannot pass a null to it ... but at the same time I don't want
to assign any values to these params in my code.

Consider the kind of solutions you'd use if it were an integer - it's
exactly the same kind of thing. You basically want a way of signalling
to the method that it shouldn't bother using that parameter. Ways of
doing that include:

o Add an extra boolean parameter which says whether or not the DateTime
parameter is valid

o Have another method which only takes one DateTime instead

o Encapsulate the DateTime in a class - look up NullableTypes on Google
for more info on this
 
B

Bret Mulvey

You could use DateTime.MinValue to represent a null DateTime. The code that
consumes this would just have to know to treat DateTime.MinValue as a
special value.

The Guid type (also a struct) has Guid.Empty which I've used a lot--I think
DateTime and other value types need the same thing, e.g. Int32.Empty.
 

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

Similar Threads

Passing DateTime 5
Check if DateTime is valid 1
a question regarding DateTime 10
DateTime Null 2
DBNULL to DATETIME 4
I couldn't see what the method would do 11
dateTime 3
Casting null values 3

Top