我已经用 C# 编写了以下代码,并希望防止每次都重新编写 try 语句。
这是在使用多个对象时减少重复代码的最佳方法,创建一次代码函数并且函数编写正确吗?
我不能在范围内使用“GoTo”,有人可以请教为什么以及如何解决这个问题吗?
Console.WriteLine("Enter DOB for member {0} - yyyy - mm - dd format", memberName);
//enter DOB for object, also create DateTime struct instance below,"d" is just a reference
DateTime d = new DateTime();
string dob = Console.ReadLine();
//pass string to convert to Date tinme
try
{
d = Convert.ToDateTime(dob);
}
catch (Exception ex)
{
Console.WriteLine("Exception in date. This record cannot be stored");
goto again1;
}
Console.WriteLine("Enter Date of Order {0} - yyyy - mm …Run Code Online (Sandbox Code Playgroud) c# ×1