By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

DateTime parsing

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
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.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 22 '05 #2

P: n/a
http://www.dotnet247.com/247referenc.../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" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
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?
Thanks for your help

Simon

Nov 22 '05 #3

P: n/a

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
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.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 22 '05 #4

P: n/a
http://www.dotnet247.com/247referenc.../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" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
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?
Thanks for your help

Simon

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.