?
Solved

when a method expect a DateTime, how can i pass it a null value?

Posted on 2009-02-20
11
Medium Priority
?
210 Views
Last Modified: 2012-05-06
hello,
i've created a method that can receive datetime in order to specify the query.

public DataTable getStats( bool dateArray, dateTime start, dateTime stop)

i'd like to call my method as follow:
- getStats(false, null, null);    <- no array, i want all values.
- getStats(true, DateTime, DateTime);   <- with array , i want to limit with dates.

now when i call my method with null, my compilation pass not,
what would be the right way to do it?


thanks in advance
0
Comment
Question by:toshi_
  • 5
  • 3
  • 3
11 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23691631
You could use method OverLoading
Not sure if that is what you need - depends on what you are trying to achieve?
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23691650
Eg:

public static void GetStats(bool myBool)
{
    //Do Something
}

public static void GetStats(bool myBool, DateTime dateFrom, DateTime dateTo)
{
    //Do Something
}
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23691689
This may also be of use
http://articles.techrepublic.com.com/5100-10878_11-1050652.html
And finally you may want to look into Nullable types:
http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
Sorry should have put all this in one post but just kept thinking of other options!
 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:toshi_
ID: 23691698
the thing is that the second would just get a line with
" AND date => ....  AND date <= ....."
as it's the only thing


here 's my method , if you can help on how OverLoading.


       public DataTable FetchAll_BagsForKgStats(bool smallPath, bool dateArray, DateTime dateStart, DateTime dateStop)
        {
            StringBuilder _query = new StringBuilder();
            _query.Append("SELECT ");
            _query.Append("case ");
 
            _query.Append("when weight = 0 then 'null' "); // 0 kg 
 
            if(smallPath)
            {
                _query.Append("when weight between 1 and 9      then '001:0.050 -> 0.500[kg]' "); // 0.050 -> 0.500 
                _query.Append("when weight between 10 and 19    then '010:0.500 -> 0.950[kg]' "); // 0.500 -> 0.950
                _query.Append("when weight between 20 and 29    then '020:1.000 -> 1.450[kg]' "); // 1.000 -> 1.450
                _query.Append("when weight between 30 and 39    then '030:1.500 -> 1.950[kg]' "); // 1.500 -> 1.950
                _query.Append("when weight between 40 and 49    then '040:2.000 -> 2.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 50 and 59    then '050:2.500 -> 2.950[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 60 and 69    then '060:3.000 -> 3.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 70 and 79    then '070:3.500 -> 3.950[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 80 and 89    then '080:4.000 -> 4.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 90 and 99    then '090:4.500 -> 4.950[kg]' "); // 2.000 -> 2.450
            }else
            {
                _query.Append("when weight between 1 and 19     then '001:0.050 -> 0.950[kg]' ");
                _query.Append("when weight between 20 and 39    then '020:1.000 -> 1.950[kg]' "); // 
                _query.Append("when weight between 40 and 59    then '040:2.000 -> 2.950[kg]' ");
                _query.Append("when weight between 60 and 79    then '060:3.000 -> 3.950[kg]' ");
                _query.Append("when weight between 80 and 99    then '080:4.000 -> 4.950[kg]' ");
            }
            _query.Append("when weight between 100 and 119  then '100:5.000 -> 5.950[kg]' ");
            _query.Append("when weight between 120 and 139  then '120:6.000 -> 6.950[kg]' ");
            _query.Append("when weight between 140 and 159  then '140:7.000 -> 7.950[kg]' ");
            _query.Append("when weight between 160 and 179  then '160:8.000 -> 8.950[kg]' ");
            _query.Append("when weight between 180 and 199  then '180:9.000 -> 9.950[kg]' ");
            _query.Append("when weight between 200 and 319  then '200:10.000 -> 10.950[kg]' ");
            _query.Append("when weight between 220 and 239  then '220:11.000 -> 11.950[kg]' ");
            _query.Append("when weight between 240 and 259  then '240:12.000 -> 12.950[kg]' ");
            _query.Append("when weight between 260 and 379  then '260:13.000 -> 13.950[kg]' ");
            _query.Append("when weight between 280 and 299  then '280:14.000 -> 14.950[kg]' ");
            _query.Append("when weight between 300 and 319  then '300:15.000 -> 15.950[kg]' ");
 
 
            _query.Append("ELSE 'other' ");
            _query.Append("END AS weight_range, COUNT(*)  ");
            _query.Append("FROM trash_bag  ");
            if(dateArray)
            {
                _query.Append("WHERE date => " + dateStart + " and date <= " + dateStop);
            }
            _query.Append("GROUP BY 1 ORDER BY weight_range ");
 
            DataTable _dt =  dba_.ExecuteQuery(_query.ToString());
 
            //DataSet _stats = new DataSet();
            //_stats.Tables.Add(_dt);
            //return _stats;
            return _dt;
        }

Open in new window

0
 
LVL 22

Expert Comment

by:p_davis
ID: 23691720
you could use

DateTime?

the question mark makes datetime nullable so you can pass a null as a parameter or a datetime.
0
 

Author Comment

by:toshi_
ID: 23691746
p_davis,

in the interface, method definition or at the call?
0
 
LVL 19

Assisted Solution

by:daveamour
daveamour earned 600 total points
ID: 23691761
Yes nullable types are an option (as I posted earlier) eg see attached code.
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace EEConsoleApp
{
    class NullableTypes
    {
        public static void Main()
        {
            DateTime? d = null;
 
            DateTime e = DateTime.Now;
 
            Test(d);
            Test(e);
 
            Console.Read();
        }
 
        public static void Test(DateTime? d)
        {
            Console.WriteLine(d);
        }
    }
}

Open in new window

0
 
LVL 22

Accepted Solution

by:
p_davis earned 1400 total points
ID: 23691769
if you want to pass nulls to the method then the method would have to hold parameters of  type (DateTime?)

and if that method is from the implementation of an interface then it would have to be prototyped that way as well.  

the only things that you can pass to datetime? are, either a datetime or a null.
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23691784
Nullable types for your code may look something like this but feel free to tinker with it of course.
       public DataTable FetchAll_BagsForKgStats(bool smallPath, bool dateArray, DateTime? dateStart, DateTime? dateStop)
        {
            StringBuilder _query = new StringBuilder();
            _query.Append("SELECT ");
            _query.Append("case ");
 
            _query.Append("when weight = 0 then 'null' "); // 0 kg 
 
            if(smallPath)
            {
                _query.Append("when weight between 1 and 9      then '001:0.050 -> 0.500[kg]' "); // 0.050 -> 0.500 
                _query.Append("when weight between 10 and 19    then '010:0.500 -> 0.950[kg]' "); // 0.500 -> 0.950
                _query.Append("when weight between 20 and 29    then '020:1.000 -> 1.450[kg]' "); // 1.000 -> 1.450
                _query.Append("when weight between 30 and 39    then '030:1.500 -> 1.950[kg]' "); // 1.500 -> 1.950
                _query.Append("when weight between 40 and 49    then '040:2.000 -> 2.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 50 and 59    then '050:2.500 -> 2.950[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 60 and 69    then '060:3.000 -> 3.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 70 and 79    then '070:3.500 -> 3.950[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 80 and 89    then '080:4.000 -> 4.450[kg]' "); // 2.000 -> 2.450
                _query.Append("when weight between 90 and 99    then '090:4.500 -> 4.950[kg]' "); // 2.000 -> 2.450
            }else
            {
                _query.Append("when weight between 1 and 19     then '001:0.050 -> 0.950[kg]' ");
                _query.Append("when weight between 20 and 39    then '020:1.000 -> 1.950[kg]' "); // 
                _query.Append("when weight between 40 and 59    then '040:2.000 -> 2.950[kg]' ");
                _query.Append("when weight between 60 and 79    then '060:3.000 -> 3.950[kg]' ");
                _query.Append("when weight between 80 and 99    then '080:4.000 -> 4.950[kg]' ");
            }
            _query.Append("when weight between 100 and 119  then '100:5.000 -> 5.950[kg]' ");
            _query.Append("when weight between 120 and 139  then '120:6.000 -> 6.950[kg]' ");
            _query.Append("when weight between 140 and 159  then '140:7.000 -> 7.950[kg]' ");
            _query.Append("when weight between 160 and 179  then '160:8.000 -> 8.950[kg]' ");
            _query.Append("when weight between 180 and 199  then '180:9.000 -> 9.950[kg]' ");
            _query.Append("when weight between 200 and 319  then '200:10.000 -> 10.950[kg]' ");
            _query.Append("when weight between 220 and 239  then '220:11.000 -> 11.950[kg]' ");
            _query.Append("when weight between 240 and 259  then '240:12.000 -> 12.950[kg]' ");
            _query.Append("when weight between 260 and 379  then '260:13.000 -> 13.950[kg]' ");
            _query.Append("when weight between 280 and 299  then '280:14.000 -> 14.950[kg]' ");
            _query.Append("when weight between 300 and 319  then '300:15.000 -> 15.950[kg]' ");
 
 
            _query.Append("ELSE 'other' ");
            _query.Append("END AS weight_range, COUNT(*)  ");
            _query.Append("FROM trash_bag  ");
 
            if(dateStart.HasValue && dateStop.HasValue)
            {
                _query.Append("WHERE date => " + dateStart + " and date <= " + dateStop);
            }
            _query.Append("GROUP BY 1 ORDER BY weight_range ");
 
            DataTable _dt =  dba_.ExecuteQuery(_query.ToString());
 
            //DataSet _stats = new DataSet();
            //_stats.Tables.Add(_dt);
            //return _stats;
            return _dt;
        }

Open in new window

0
 

Author Comment

by:toshi_
ID: 23691791
grate!!!!!

i put the question mark in my interface and also in my inherited method,
and it goes smoothly ;o)


thanks a lot experts !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 22

Expert Comment

by:p_davis
ID: 23691807
_query.Append("WHERE date => " + dateStart + " and date <= " + dateStop);


probably should either cast with (datetime) or you have to use the value--

_query.Append("WHERE date => " + dateStart.Value + " and date <= " + dateStop.Value);// this might have to be tostring??
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question