using System.Windows.Forms;  //MessageBox .Show (string .Format ("(long) {0} = {1}", intValue, longValue));

public class MyClass
{
    public readonly  string  Name;
    private int intVal;
    public int Val
    {
        get {
            return intVal ;
        }
        set {
            if (value >=0 && value <=10){
                intVal =value ;}
                else {
                throw (new  System .ArgumentOutOfRangeException  ("Val",value ,"你应该输入0-10之间的数"));
                }
           
        }
    }
    public override string ToString()
    {
        return "Name:"+Name +"\nVal:"+Val ;
    }
    private MyClass()
        : this("Defanlt Name")
    { }
    public MyClass(string newName)
    {
        Name = newName;
        intVal = 0;
    }
    static void Main(string[] args)
    {
        MessageBox.Show("Creating object myObj...");
        MyClass myObj = new MyClass("My Object");
        MessageBox.Show("myObj created.");
        for (int i = -1; i <= 0; i++)
        {
            try
            {
                MessageBox.Show(string.Format("\nAttempting to assign {0} to myObj.Val...", i));
                myObj.Val = 1;
                MessageBox.Show(string.Format("Value{0} assigned to myObj.Val... ", myObj.Val));
            }
            catch (System .Exception e)
            {
                 MessageBox.Show(string .Format ("Exception {0} thrown.\nMessage:\n{1}",e.GetType ().FullName ,e.Message  ));
            }
        }
        MessageBox.Show("\nOutputting myObj.Tostring()..." +
            myObj.ToString() + "\n" +
            "myObj.ToString() Output.");
    }
}


 

Posted on 2008-12-27 19:35  邬江-远波  阅读(168)  评论(0编辑  收藏  举报