似乎这个特定的错误已经解决了很多次,但我的代码片段有一些不同之处在于它永远不会导致“未分配”错误。
这段代码来自我为学校做的一个项目。我被允许寻求帮助,这是我希望在这里找到的。我不在乎掩盖任何变量或其他任何东西,因为它不是出于商业目的。
这是编译时的错误:“使用未分配的局部变量'dateStartedActual'”
switch (userType)
{
case "Doctor":
string qualification = Microsoft.VisualBasic.Interaction.InputBox("What is the highest qualification this person has", "Qualification", "", -1, -1);
while (dateStarted == "")
{
try
{
dateStarted = Microsoft.VisualBasic.Interaction.InputBox("On which date did this person start", "Date Started", "", -1, -1);
int day = Convert.ToInt32(Regex.Match(dateStarted, @"\d{2}").Value);
dateStarted.Remove(0,3);
int month = Convert.ToInt32(Regex.Match(dateStarted, @"\d{2}").Value);
dateStarted.Remove(0,3);
int year = Convert.ToInt32(Regex.Match(dateStarted, @"\d{4}").Value);
dateStartedActual = new DateTime(day, month, year);
}
catch (Exception ex)
{
MessageBox.Show("The date entered is not valid");
dateStarted = "";
}
}
string field = Microsoft.VisualBasic.Interaction.InputBox("In which field does this person practice", "Field", "", -1, -1);
CreateDoctor(qualification, dateStartedActual, field);
break;