Solved

is it possible to change from DateTime?  to DateTime?

Posted on 2009-04-02
5
262 Views
Last Modified: 2013-12-17
hello experts,

i have a method where values can be null, the params are DateTime?
inside  my method, I call another method with DateTime values that i check first if they are not null,
the inner method has not DateTime? params,

how can i change from "DateTime?" to "DateTime" ?
0
Comment
Question by:toshi_
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:philipjonathan
Comment Utility
The question is what datetime value should you pass into the inner method, if the outer method receives null DateTime?

One way is to pass in DateTime.MinValue:
if (dateTime == null)
  InnerMethod(DateTime.MinValue);

Another way is to skip calling InnerMethod altogether if DateTime is null:
if (dateTime != null)
  InnerMethod(dateTime);
0
 

Author Comment

by:toshi_
Comment Utility
thank you for your answer,
i didnot succed with your help,

here's the situation.

i have an object drain wich contain the following getter:

OBJET DRAIN
 public DateTime?  DrainDate
        {
            set { DrainDate_ = value ; }
            get
            {
                return ((DrainDate_ != null)? DrainDate_ : DateTime.MinValue);
            }
           
        }

when i try to get the value i receive the error : cannot  convert from nullable datetime to target datetime.

  foreach (Drain currentDrain in _Drains)
                {
                    DateTime _currentDrainDate = currentDrain.DrainDate;

this last line refuses to accept my value.
any idea, on how to do it?
0
 

Author Comment

by:toshi_
Comment Utility
ok, i could change the line like this:
foreach (Drain currentDrain in _Drains)
                {
                    DateTime? _currentDrainDate = currentDrain.DrainDate;

and it will pass,
the problem is that i use _currentDrainDate later on my inner method,
and the method i give it to ,  is not declare with a parameter of type "DateTime?" but "DateTime"
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 250 total points
Comment Utility
If that's the case, you can just use cast. Try:

foreach (Drain currentDrain in _Drains)
{
    DateTime _currentDrainDate = (DateTime) currentDrain.DrainDate;
0
 

Author Comment

by:toshi_
Comment Utility
thank you very much,

i didnt think about trying that,
i try several things but not that cast,
"IT DID IT" , as we say in french switzerland ;o)

best regards.


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now