1
Answer

Property in C#

Photo of Arezoo Sh

Arezoo Sh

13y
1.7k
1
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?

 

Answers (1)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
Try this:

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();  
   }
}


Next Recommended Forum