DateTime parsing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 to use a static method in a static object to convert the dateTime to DateTime datatype as follows

public static DateTime ParseDate(String dateTime

tr

return DateTime.Parse(dateTime)

catc

return null; // Not sure about what to return, error occurs

}

Does the use of the static method cause the performance of the application decrease? if so, for the case above, should I just put the content of the static method at the original method(the point of use)
Would it be always a good practice not to make any assumption and test the validity of the parameter of the method

Thanks for your hel

Simon
 
Simon said:
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 to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application
decrease?

No.

if so, for the case above, should I just put the content of the static
method at the original method(the point of use)?
Would it be always a good practice not to make any assumption and test the
validity of the parameter of the method?

Sure, you might test it for null value or empty string - depends on your
needs.
 
http://www.dotnet247.com/247reference/msgs/17/85604.aspx has some code for
this as well. The method there returns a boolean, which is a better fit for
IsDate().

Otherwise you may be better off just using DateTime.Parse() or
Convert.ToDateTime() instead of having this method. With your current method
you still have to check for a null return, so you might as well just not
have the method and do exception handling in main code (especially since
you'd be doing it anyway in the method).

Simon said:
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 to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application
decrease? if so, for the case above, should I just put the content of the
static method at the original method(the point of use)?
Would it be always a good practice not to make any assumption and test the
validity of the parameter of the method?
 
Back
Top