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

Testing data type

P: n/a
AAJ
Hi

has anyone come across a function to check if a particular string can be
safely converted to a datatype

i.e. i would like to check things like

TypeCheck("1/1/2006",datetime) -returns true

TypeCheck("fred",datetime) -returns false

Typecheck("100"),int32) -returns true

Typecheck("100.121"),int32) -returns false

etc....
I know I could write by own by testing within a try{} block, but I wonder if
anyone already has one already tested and working

Andy
Nov 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Andy,

In the 2.0 framework many Types (including primitives) now have TryParse
methods. I really don't think there is any other way to see if a string value
is of a certain format without attempting to parse it.

In earlier versions of the framework I would perform the parsing on a case by
case basis, but I can see how a simple utility may have come in handy.
Writing a utility for this is trivial, so if you find yourself parsing strings
in many different places in code then you might want to consider writing your
own.

This is the signature commonly used by the 2.0 classes, in case you're
wondering:

public static bool TryParse(string s, out [type] result)

IMO, the sig could be improved for checking whether a string can be parsed
without having to store the result in a variable. But even now it's much
nicer than having to write Try...Catch statements all over the place :)

--
Dave Sexton

"AAJ" <a.a.comwrote in message news:u9**************@TK2MSFTNGP04.phx.gbl...
Hi

has anyone come across a function to check if a particular string can be
safely converted to a datatype

i.e. i would like to check things like

TypeCheck("1/1/2006",datetime) -returns true

TypeCheck("fred",datetime) -returns false

Typecheck("100"),int32) -returns true

Typecheck("100.121"),int32) -returns false

etc....
I know I could write by own by testing within a try{} block, but I wonder if
anyone already has one already tested and working

Andy

Nov 2 '06 #2

P: n/a
Well, on a 1-by-1 basis you can look at the TryParse signature in 2.0 -
however this is static so not that re-usable in a generic sense. For a more
generic approach, TypeConverter may be of use:

using System;
using System.ComponentModel;
class Program
{
static void Main()
{
Console.WriteLine(CanConvert<DateTime>("Fred"));
Console.WriteLine(CanConvert<DateTime>("1/1/2006"));
Console.WriteLine(CanConvert<int>("100"));
Console.WriteLine(CanConvert<int>("100.134"));

}
// 2.0 variant
private static bool CanConvert<TTo>(object valueFrom)
{
return CanConvert(typeof(TTo), valueFrom);
}
// 1.1 friendly
private static bool CanConvert(Type typeTo, object valueFrom)
{
// TODO: check for null params first...
TypeConverter converter = TypeDescriptor.GetConverter(typeTo);
Type typeFrom = valueFrom.GetType();
if (converter == null || !converter.CanConvertFrom(typeFrom) ||
!converter.IsValid(valueFrom))
{
return false;
}
try {
converter.ConvertFrom(valueFrom); // test the conversion
return true;
} catch {
return false; // damnit!
}
}
}
Nov 2 '06 #3

P: n/a
AAJ
Thanks guys

I'm running 2.0 and I shall give the suggestions a try

cheers again
Andy
"AAJ" <a.a.comwrote in message
news:u9**************@TK2MSFTNGP04.phx.gbl...
Hi

has anyone come across a function to check if a particular string can be
safely converted to a datatype

i.e. i would like to check things like

TypeCheck("1/1/2006",datetime) -returns true

TypeCheck("fred",datetime) -returns false

Typecheck("100"),int32) -returns true

Typecheck("100.121"),int32) -returns false

etc....
I know I could write by own by testing within a try{} block, but I wonder
if anyone already has one already tested and working

Andy

Nov 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.