希望我的方法(秒方法)被执行并调用到我的主类

时间:2015-10-06 12:25:11

标签: c# .net

    class Phone
    {
        //VARIABLES
        public string phoneNumber;
        public int size;
        public int found;//used to search for the numbers in the phone book
        Dictionary<string, string> savedNumbers = new Dictionary<string, string>();
        public string[] savedName;

        private Dictionary<string, string> FormatPhoneBook(string toClean)
        {
            string temp = toClean.Replace("{", "").Replace("}", "");
            Dictionary<string, string> returnValue = new Dictionary<string, string>();
            List<string> tempPhoneBook = temp.Split(';').ToList();
            foreach (var item in tempPhoneBook)
            {
                string[] tempVal = item.Split('=');
                returnValue.Add(tempVal[0], tempVal[1]);
            }
            return returnValue;
        }
        public Phone()
        {
            phoneNumber = "";
            size = 0;
            found = -1;//used to search for the numbers in the phone book
            string numbersFromConfig = ConfigurationManager.AppSettings["StoredNumbers"].ToString();

            savedNumbers = FormatPhoneBook(numbersFromConfig);
        }

        public Phone(string _phoneNumber, int _size, int _found)
        {
            phoneNumber = _phoneNumber;
            size = _size;
            found = _found;//used to search for the numbers in the phone book

        }

        public string secMethod() //I want this method to strip out the "(",")","-" and a space character.
        {
            for (int i = 1; i < phoneNumber.Length; i++)
            {
                if (phoneNumber.Contains("(") || phoneNumber.Contains(")") || phoneNumber.Contains("-"))
                {
                    string tempNumber = phoneNumber.Replace("(", "");
                    phoneNumber = tempNumber;
                    tempNumber = phoneNumber.Replace(")", "");
                    phoneNumber = tempNumber;
                    tempNumber = phoneNumber.Replace("-", "");
                    phoneNumber = tempNumber;

                    Console.WriteLine("Successfully dialed {" + phoneNumber + "}");
                }
            }

            return phoneNumber;
        }

此方法检查输入的数字是否包含10个数字,不包括 - ,)的字符(和空格。最后它返回10个数字值而不包含任何其他字符

        public string ManipulateContac() 
        {
            secMethod();
            do
            {
                //secMethod();
                Console.WriteLine("\nPlease enter the number you wish to dial");
                phoneNumber = Console.ReadLine();//capture the entered number 

                size = phoneNumber.Length;

                if (size != 10 || size > 10)
                {
                    Console.WriteLine("Entered nubmers should be 10 digits long");
                    size = 0;
                }
                else
                {
                    HERE I CHECK IF THE ENTERED NUMBER STARTS WITH 012 , 011, 083, 072, 069 OR 073 
                    if (phoneNumber.StartsWith("012") || phoneNumber.StartsWith("083") || phoneNumber.StartsWith("069") ||
                        phoneNumber.StartsWith("011") || phoneNumber.StartsWith("072") || phoneNumber.StartsWith("073"))
                    {
                        check if its all digits
                        if (phoneNumber.All(Char.IsDigit))
                        {
                            if (savedNumbers.ContainsKey(phoneNumber))
                            {
                                Console.WriteLine("Successfully dialed {" + savedNumbers[phoneNumber] + "}");
                            }
                            else
                            {
                                Console.WriteLine("Successfully dialed {" + phoneNumber + "}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid number, Please use numeric digits only.");
                            size = 0;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Only numbers starting with 012 , 011, 083, 072, 069 and 073 are allowed");
                        size = 0;
                    }
                }
            } while (size < 10);
            return phoneNumber;
        }

        class Program
        {
            static void Main(string[] args)
            {


                Phone phoneOBJ = new Phone();

                //Console.WriteLine("\nPlease enter the number you wish to dial");
                //phoneOBJ.phoneNumber = Console.ReadLine();//capture the entered number 
                //string phone_number = "";
                //phone_number = phone_number;
I WANT TO PROMPT THE USER FROM HERE IN MY MAIN CLASS INSTEAD OF PROMPTING IN MY "secMethod()"
                phoneOBJ.secMethod(); CALLING THE secMethod METHOD
                phoneOBJ.ManipulateContac(); CALLING THE ManipulateContact METHOD
                Console.ReadKey();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用事件或回调。

通过事件,您可以在班级KeyPoint中声明事件,而不是

phone

You call your event

使用回调,您可以将回调方法传递到您的班级或Console.WriteLine("Successfully dialed {" + phoneNumber + "}"); 本身。例如

secMethod()

并且在调用类secMethod(Action<string> callback) { . . . . . callback(phoneNumber); } 中你可以有类似的东西

Program