Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Cannot implicitly convert type 'string' to 'System.DateTime?

Is there a simple way to convert a data field that has be defined as a string into an actual datetime?
myCRpatient.DECEASED_DATE = patient.p.deceasedDate;---------squiggly under patient.p.deceasedDate

Open in new window

Avatar of oxyoo
oxyoo
Flag of Sweden image

DateTime.Parse & DateTime.TryParse lets you convert a String into a DateTime object.
More information about those methods here...
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx


Good Luck!
Avatar of kwh3856

ASKER

oxyoo
I tried

myCRpatient.DECEASED_DATE = DateTime.TryParse(patient.p.deceasedDate);
It then told me no overload for method.
The syntax is as follows
if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

Either use the syntax above (suggested by Tiggerito) which is more "safe" or use:


myCRpatient.DECEASED_DATE = DateTime.Parse(patient.p.deceasedDate);

Open in new window

kwh3856,

I see that you have a few of these posts for multiple data type conversions.  Most (all?) data type classes and structures in .NET have a TryParse() method that accepts a string to convert and a variable on which to store the converted value.  The method returns true if parsing succeeded, or false if it failed.  For example:
    if (Int32.TryParse(myString, out myInt))
    {
        // Success! myInt now contains the numeric value of myString
    }
    else
    {
        // Failed! MyInt is not defined.
    }

The TryParse() method only converts from strings to other types.  For conversions between different types, you can use the Convert class.  It has methods to convert to multiple types, such as:
   Convert.ToString()
   Convert.ToInt32()
   Convert.ToByte()
   Convert.ToDate()
   Convert.ToDouble()
   // etc.

Each method is overloaded to accept most data types.  Here's more information on these methods:
    http://articles.techrepublic.com.com/5100-10878_11-5834604.html

And lastly, if you want to convert anything to a string, this is even easier! Every object has a ToString() method that "stringifies" its value.  For example, to convert an Integer to a string, use:
     myIntegerValue.ToString()
And to convert a DateTime value to a string, use:
     myDateTimeValue.ToString()

     I hope this helps.
     -dZ.
Avatar of kwh3856

ASKER

tiggerito,
When I tried your code I got this error message
A property or indexer may not be passed as an out or ref parameter
 

if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))------ squiggle under myCRpatient.Deceased_date
 
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

Avatar of kwh3856

ASKER

tiggerito,
I tried this and got an invalid arguments error message.

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out myCRpatient.DECEASED_DATE))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

If DECEASED_DATE is a property you can not use it with the out keyword. Properties are a kind of syntactic sugar methods and methods are not allowed to be used in conjunction with out.
Try this instead:

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

Avatar of kwh3856

ASKER

oxyoo,
When I tried your example I got
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
That occured here

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;  ------ squiggle under myCRpatient.DECEASED_DATE
 
and
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
This occured here

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))---squiggle line from DateTime all the the way to the last parenthesis
 
Hi,
This means that myCRpatient.DECEASED_DATE is nullable. I have adapted the code below (I made the DateTime nullable), and it should work now:
Read about nullables here:
http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx


DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

Avatar of kwh3856

ASKER

oxyoo,
That fixed the first error message but I am still getting the same second error message.  Any ideas?
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
Thanks
Kenny
 
ToString should be ToString()
ASKER CERTIFIED SOLUTION
Avatar of oxyoo
oxyoo
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

oxyoo,
Thanks for the update but I still get the error on
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
error occurs on this line

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime2))
I am still trying to digest the information your giving me so I am not sure what to try to fix it.
Avatar of kwh3856

ASKER

Got it... i read your above post.....had to add parens to the tostring command

DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (tmpDateTime.HasValue)
{
DateTime tmpDateTime2 = tmpDateTime.Value;
if (DateTime.TryParse(patient.p.deceasedDate.ToString(), out tmpDateTime2))
{
// success, valid date format
}
else
{
// failed, invalid date format
}
}
else
{
// failed, DateTime is null
}
Avatar of kwh3856

ASKER

Thank you very much for sticking out with me.  I am still trying to digest the code but I just need to chew on for awhile and I will understand what you did.  Thank you very much.