hi
I want to write a programme that the string just get the numbers Not characters (property) with C# in Console. can anybody help me plz?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 24, 2011, 5:55 AM
using System;
class MyClass
{
string numericString = "0"; // default value
public string NumericString
{
get { return numericString; }
set
{
double number;
bool isValid = double.TryParse(value, out number);
if (isValid)
numericString = number.ToString();
else
throw new ArgumentException(String.Format("'{0}' is not a numeric string", value));
}
}
}
class Program
{
static void Main()
{
MyClass mc = new MyClass();
try
{
mc.NumericString = "12.34";
mc.NumericString = "abc";
}
catch(ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}