如何从另一个类C#的属性数组

时间:2014-02-21 05:36:51

标签: facebook

 class Customer:PhoneNumber
    {
        //PhoneNumber p = new PhoneNumber();
        private string fname;
        private string lname;
        private string cname;
        private string pnumber;
        private string type;
        private string anum;
        private string phonenumber;

       public  PhoneNumber[] a ;





        public string sFirstName
        {
            get
            {

                return fname;

            }
            set
            {
                fname = value;
            }


        }
        public string sLastName
        {
            get
            {
                return lname;
            }
            set
            {
                lname = value;
            }

        }
        public string sCompanyName
        {
            get
            {
                return cname;
            }
            set
            {
                cname = value;
            }


        }




        public PhoneNumber[] p
        {
            get
            {
  ?????????????????????
           How to do it here?

            }

        }

        public Customer()
        {
            sFirstName = fname;
            sLastName = lname;
            sCompanyName = cname;
           //phone = pnumber;


        }
        public Customer(string Fname,string Lname,string Cname)
        {
            sFirstName = Fname;
            sLastName = Lname;
            sCompanyName = Cname;

        }
        public Customer(string Fname, string Lname, string Cname,string phonenumber)
        {
            sFirstName = Fname;
            sLastName = Lname;
            sCompanyName = Cname;

            AddPhoneNumber(phonenumber,"fds");



        }
        public string AddPhoneNumber(string pnum1,string pnum2)
        {

           sPhoneNumber = pnum1;
            if (pnum2 == null)
            {

                sPhoneType = "Home";
            }
            else
            {

               sPhoneType = pnum2; ;

            }
            return sPhoneNumber + sPhoneType;
        }
        public override string ToString()
        {
            return string.Format("{0} {1},{2} ",sFirstName,sLastName,sCompanyName);
        }
        public void DisplayCustomer()
        {
            Console.WriteLine(ToString());


            foreach (var num in p)
            {

                Console.WriteLine(num);
            }
              //PhoneNumber ph = new PhoneNumber(sPhoneNumber,sPhoneType);

              //Console.WriteLine(ph.ToString());
            }


        }
 class PhoneNumber
    {

        public string sPNumber;
       public string sPType;
        public int count=0;
        public string sPhoneNumber
        {
            get
            {
                return sPNumber;
            }
            set
            {
                //sPNumber = value;
                bool check = VerifyPhoneNumber(value);
                if (check == true)
                {
                    sPNumber = value;
                }
                else
                {
                    //Console.WriteLine("Wrong Number");
                    //string m = "Default number is (000) 000‐0000";
                    //m = value;
                    sPNumber = "Default number is (000) 000‐0000";
                    //Console.WriteLine("Default number is (000) 000‐0000");


                }
                count++;
            }


        }

        public string sPhoneType
        {
            get
            {
                return sPType;
            }

            set
            {
                sPType = value;
            }

        }

        public PhoneNumber()
        {
           sPNumber="";
           sPType="";
        }
        public PhoneNumber(string pNumber)
        {
            this.sPhoneNumber = pNumber;
        }
        public PhoneNumber(string pNumber, string pType)
        {
            sPhoneNumber= pNumber;
            sPhoneType = pType;

        }
        public bool VerifyPhoneNumber(string pnumber)
        {
            if (pnumber.Contains( "0") || pnumber.Contains( "1" )|| pnumber.Contains( "2") || pnumber .Contains( "3") || pnumber .Contains( "4" )|| pnumber.Contains( "5") || pnumber .Contains( "6") || pnumber.Contains( "7") || pnumber.Contains("8") || pnumber.Contains( "9"))
            {

                return true;
            }
            else
            {
                return false;
            }



        }
        public override string ToString()
        {
            return string.Format("{0}:{1}",sPhoneType,sPhoneNumber);
        }


    }

2 个答案:

答案 0 :(得分:0)

PhoneNumber p = new PhoneNumber();

上面的p不是数组。

PhoneNumber []pArray = new PhoneNumber[3];

public PhoneNumber[] p
{
        get
        {
            //return value should be an array of PhoneNumber
            return pArray;
        }
}

答案 1 :(得分:0)

您是否为pArray分配了值? 你必须指定如下,

pArray[0] = new PhoneNumber();
pArray[1] = new PhoneNumber();
pArray[2] = new PhoneNumber();