C# 希望我的方法(sec方法)被导出并调用到我的主类 class电话 { //变数 公共字符串电话号码; 公共整数大小; 找到public int;//用于搜索电话簿中的号码 Dictionary savedNumbers=新字典(); 公共字符串[]savedName; 专用字典格式电话簿(字符串清理) { 字符串temp=toClean.Replace(“{”,”).Replace(“}”,”); Dictionary returnValue=新字典(); List tempPhoneBook=temp.Split(“;”).ToList(); foreach(tempPhoneBook中的变量项) { 字符串[]tempVal=item.Split('='); returnValue.Add(tempVal[0],tempVal[1]); } 返回值; } 公用电话 { 电话号码=”; 尺寸=0; found=-1;//用于在电话簿中搜索号码 string numbersFromConfig=ConfigurationManager.AppSettings[“StoredNumber”].ToString(); SavedNumber=FormatPhoneBook(numbersFromConfig); } 公用电话(字符串电话号码、整数大小、整数已找到) { phoneNumber=_phoneNumber; 大小=_大小; found=\u found;//用于在电话簿中搜索号码 } 公共字符串secMethod()//我希望此方法去掉“(“,”),“-”和一个空格字符。 { for(int i=1;i10) { Console.WriteLine(“输入的数字长度应为10位”); 尺寸=0; } 其他的 { 在这里,我检查输入的数字是否以012、011、083、072、069或073开头 if(phoneNumber.StartsWith(“012”)| phoneNumber.StartsWith(“083”)| phoneNumber.StartsWith(“069”)|| phoneNumber.StartsWith(“011”)| | phoneNumber.StartsWith(“072”)| | phoneNumber.StartsWith(“073”)) { 检查它是否为所有数字 if(phoneNumber.All(Char.IsDigit)) { if(savedNumbers.ContainsKey(phoneNumber)) { WriteLine(“成功拨打了{”+savedNumbers[phoneNumber]+“}”); } 其他的 { WriteLine(“成功拨打了{“+phoneNumber+”}”); } } 其他的 { Console.WriteLine(“无效数字,请仅使用数字。”); 尺寸=0; } } 其他的 { Console.WriteLine(“仅允许以012、011、083、072、069和073开头的数字”); 尺寸=0; } } }而(尺寸
此方法检查输入的数字是否包含10位数字,不包括字符a-、)、(和空格。最后返回10个数值,不包含任何其他字符C# 希望我的方法(sec方法)被导出并调用到我的主类 class电话 { //变数 公共字符串电话号码; 公共整数大小; 找到public int;//用于搜索电话簿中的号码 Dictionary savedNumbers=新字典(); 公共字符串[]savedName; 专用字典格式电话簿(字符串清理) { 字符串temp=toClean.Replace(“{”,”).Replace(“}”,”); Dictionary returnValue=新字典(); List tempPhoneBook=temp.Split(“;”).ToList(); foreach(tempPhoneBook中的变量项) { 字符串[]tempVal=item.Split('='); returnValue.Add(tempVal[0],tempVal[1]); } 返回值; } 公用电话 { 电话号码=”; 尺寸=0; found=-1;//用于在电话簿中搜索号码 string numbersFromConfig=ConfigurationManager.AppSettings[“StoredNumber”].ToString(); SavedNumber=FormatPhoneBook(numbersFromConfig); } 公用电话(字符串电话号码、整数大小、整数已找到) { phoneNumber=_phoneNumber; 大小=_大小; found=\u found;//用于在电话簿中搜索号码 } 公共字符串secMethod()//我希望此方法去掉“(“,”),“-”和一个空格字符。 { for(int i=1;i10) { Console.WriteLine(“输入的数字长度应为10位”); 尺寸=0; } 其他的 { 在这里,我检查输入的数字是否以012、011、083、072、069或073开头 if(phoneNumber.StartsWith(“012”)| phoneNumber.StartsWith(“083”)| phoneNumber.StartsWith(“069”)|| phoneNumber.StartsWith(“011”)| | phoneNumber.StartsWith(“072”)| | phoneNumber.StartsWith(“073”)) { 检查它是否为所有数字 if(phoneNumber.All(Char.IsDigit)) { if(savedNumbers.ContainsKey(phoneNumber)) { WriteLine(“成功拨打了{”+savedNumbers[phoneNumber]+“}”); } 其他的 { WriteLine(“成功拨打了{“+phoneNumber+”}”); } } 其他的 { Console.WriteLine(“无效数字,请仅使用数字。”); 尺寸=0; } } 其他的 { Console.WriteLine(“仅允许以012、011、083、072、069和073开头的数字”); 尺寸=0; } } }而(尺寸,c#,.net,C#,.net,此方法检查输入的数字是否包含10位数字,不包括字符a-、)、(和空格。最后返回10个数值,不包含任何其他字符 class Phone { //VARIABLES public string phoneNumber; public int size; public int found;//used to search for the numbers in the phone book Dictionar
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;
}
public string-manufacturecontac()
{
secMethod();
做
{
//secMethod();
Console.WriteLine(“\n请输入您要拨打的号码”);
phoneNumber=Console.ReadLine();//捕获输入的号码
大小=电话号码。长度;
如果(尺寸!=10 | |尺寸>10)
{
Console.WriteLine(“输入的数字长度应为10位”);
尺寸=0;
}
其他的
{
在这里,我检查输入的数字是否以012、011、083、072、069或073开头
if(phoneNumber.StartsWith(“012”)| phoneNumber.StartsWith(“083”)| phoneNumber.StartsWith(“069”)||
phoneNumber.StartsWith(“011”)| | phoneNumber.StartsWith(“072”)| | phoneNumber.StartsWith(“073”))
{
检查它是否为所有数字
if(phoneNumber.All(Char.IsDigit))
{
if(savedNumbers.ContainsKey(phoneNumber))
{
WriteLine(“成功拨打了{”+savedNumbers[phoneNumber]+“}”);
}
其他的
{
WriteLine(“成功拨打了{“+phoneNumber+”}”);
}
}
其他的
{
Console.WriteLine(“无效数字,请仅使用数字。”);
尺寸=0;
}
}
其他的
{
Console.WriteLine(“仅允许以012、011、083、072、069和073开头的数字”);
尺寸=0;
}
}
}而(尺寸<10);
返回电话号码;
}
班级计划
{
静态void Main(字符串[]参数)
{
Phone phoneOBJ=新手机();
//Console.WriteLine(“\n请输入您要拨打的号码”);
//phoneOBJ.phoneNumber=Console.ReadLine();//捕获输入的号码
//字符串phone_number=“”;
//电话号码=电话号码;
我想在主类中提示用户,而不是在“secMethod()中提示”
phoneOBJ.secMethod();调用secMethod方法
phoneOBJ.manipeContac();调用manipeContact方法
Console.ReadKey();
}
}
}
您可以使用事件或回调
使用events,您可以在类phone
中声明event,而不是
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();
}
}
}
通过callback,您可以将回调方法传递给类或secMethod()
本身
Console.WriteLine("Successfully dialed {" + phoneNumber + "}");
不需要CAPS,请进行适当的格式化。不要包含整个有问题的项目,只包含最小的代码来显示问题。@Senior,请查看我的答案非常感谢您的评论,在您的帮助下,我成功地完成了控制台应用程序,它位于git上now@Senior它帮助了你,请接受答案。
secMethod(Action<string> callback)
{
. . . . .
callback(phoneNumber);
}
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
});
}