Point.Parse("SomeText");
how can i check if the given string represent a point ?
Parse Method Documentation is Here
The fastest, and probably cleanest way to achieve this is by implementing your own TryParse
method for Point.Parse
:
public bool TryParse(string source, out Point point)
{
try
{
point = Point.Parse(source);
}
catch (FormatException ex)
{
point = default(Point);
return false;
}
return true;
}
Which you can then consume like so:
Point point;
if (!TryParse("12, 13", out point))
{
// We have an invalid Point!
}
If your string isn't a valid point, the method will return false and you can do whatever needs to be done right away. The out
parameter will contain the parsed Point
if the parse succeeded, and will otherwise contain the default value for Point
, which is probably (0, 0).
Do note that the exception is being supressed here, but that shouldn't give you any trouble. If need be, you can re-throw it after setting point
.