Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

is it possible to change from DateTime?  to DateTime?

Posted on 2009-04-02
5
Medium Priority
?
278 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24047993
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_
ID: 24048923
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_
ID: 24048954
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 1000 total points
ID: 24049822
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_
ID: 24050059
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

704 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