473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DateTime as a method parameter

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.
Nov 15 '05 #1
7 4211
"Abhishek Srivastava" <ab******@hotmail.com> schrieb im Newsbeitrag
news:af*************************@posting.google.co m...
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
Nov 15 '05 #2
"Abhishek Srivastava" <ab******@hotmail.com> schrieb im Newsbeitrag
news:af*************************@posting.google.co m...
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
Nov 15 '05 #3
Abhishek Srivastava <ab******@hotmail.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Abhishek Srivastava <ab******@hotmail.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
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.
"Abhishek Srivastava" <ab******@hotmail.com> wrote in message
news:af*************************@posting.google.co m...
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.

Nov 15 '05 #6
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.
"Abhishek Srivastava" <ab******@hotmail.com> wrote in message
news:af*************************@posting.google.co m...
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.

Nov 15 '05 #7

The NullableTypes enable DateTime, Boolean, Byte, Decimal,
etc. to accept null value.

Look at this: http://nullabletypes.sourceforge.net/

HTH (luKa)
-----Original Message-----
... I tried calling the method with
null params but that didn't work because DateTime is a struct.


Nov 15 '05 #8

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

Similar topics

2
by: Simon | last post by:
Hi all If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like IsDate in VB.Net. Would it be a good idea...
14
by: ChrisM | last post by:
Could anyone please tell me the difference between these 2 lines: myDate = DateTime.Now; and myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")); Seeing as later on in the...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
3
by: Daves | last post by:
a get { ... } for public property SelectedValue returns DateTime type to be used as a parameter in a Sql update query but I'd like it to return "empty" if no date has been selected... I cannot use...
3
by: Rich Robinson | last post by:
Hi, I have a web service method which takes a DateTime type as a parameter. The service is UK based, and the dates are passed in to the service in the UK format dd/MM/yyyy. On a recent...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am getting an Exception and trying to fix as MS suggest From C# help this comes If I try
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
2
by: joasi | last post by:
Hi, I am working in C# with an Object / Relational Database mapping framework, where I want to generate method calls based on string representation of the call signature. I have used reflection and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.