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

本文关键字:方法 我的 调用 sec 希望 执行 | 更新日期: 2023-09-27 18:16:10

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

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

您可以使用Events或callback。

对于事件,您可以在类phone中声明事件,而不是

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

调用事件

使用callback,您将回调方法传递到您的类或secMethod()本身。例如

secMethod(Action<string> callback)
{
    . . . . . 
    callback(phoneNumber);
}

在调用类Program中你可以这样写

static void Main(string[] args)
{
   . . . . . . . 
   myClass.secMethod(delegate(string phone) 
                     { 
                        // here you place code of whatever you want to do
                        // with phone number you passing from secMethod  
                     });
}