2013-11-02 57 views
1

似乎这个特定的错误已经被解决了很多次,但是我的代码片段有一些不同之处,因为它永远不会导致“未分配”错误。在Try Catch中使用未分配的局部变量

此代码来自我为学校所做的一个项目。我被允许寻求帮助,这是我希望在这里找到的。我不在乎掩盖任何变数或任何因为它不是用于商业目的。

这是在编译时错误: “未分配的局部变量的使用‘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; 

回答

1

我的代码段有一些不同之处在于它不会导致“未分配”的错误

那么很明显确实导致该错误,这就是为什么你问这个问题,不是吗?

尽管知道任何时候抛出异常,你会再次绕着循环,编译器不知道...因此错误。值为而不是已明确分配。当然,你可以给它一个虚拟的价值开始 - 但我个人不喜欢这样做。

你会更好提取解析代码放到一个单独的方法,这可能看起来像:

static DateTime RequestStartDate() 
{ 
    while (true) 
    { 
     try 
     { 
      // Ask for date and parse it 
      // ... 
      return parsedDate; 
     } 
     catch (Exception e) // See below... 
     { 
      MessageBox.Show("The date entered is not valid"); 
     } 
    } 
} 

这种方法肯定会返回一个DateTime最终,还是永远保持循环 - 因此,任何变量分配通过调用该方法将被明确分配。

然后在你的主代码,你可以写:

switch (userType) 
{ 
    case "Doctor": 
     string qualification = Microsoft.VisualBasic.Interaction.InputBox("What is the highest qualification this person has", "Qualification", "", -1, -1); 
     DateTime dateStarted = RequestStartDate(); 
     string field = Microsoft.VisualBasic.Interaction.InputBox("In which field does this person practice", "Field", "", -1, -1); 
     CreateDoctor(qualification, dateStarted, field); 
     break; 

顺便说一句,你打电话string.Remove,而忽略结果 - 总是一个坏主意。手动解析日期是不必要的复杂 - 使用DateTime.TryParseExact

此外,捕Exception通常是一个坏主意 - 你应该抓住具体例外......但如果你使用DateTime.TryParseExact你不会需要捕捉到任何,因为它只会如果该值不能返回false被解析。

我也劝你至少using指令为Microsoft.VisualBasic,这样你可以用:

string qualification = Interaction.InputBox(...); 

等,而不是每次有一个巨大的长队。

+0

它是在while循环中检查dateStarted ==为“”,它将被设置为catch中的值。所以同时将再次从顶部启动try catch块。如果日期不正确,它将永远不会到达CreateDoctor – Aernor

+0

@Anor:是的,意识到并编辑。 –

+0

谢谢,我会尝试。感谢您的及时回复 – Aernor

2

有两种方法可以解决这个错误。

首先

分配在catch块dateStartedActual一些值。

OR

提供try块之前的dateStartedActual一些默认值。在这种情况下,如果在try块中有任何异常,您的dateStartedActual将具有您提供的默认值。

+0

这似乎是一个解决错误的好方法,但我宁愿让输入框要求用户填写正确的日期,而不是输入默认值到我的数据库 – Aernor

+0

@Aeror:你错过了点 - 你已经循环,所以默认值不会被实际使用。但我仍然认为这是一个丑陋的方法 - 我肯定会将它提取到不同的方法。 –

+0

我想投这个答案,因为它是我的解决方案的一部分,但我是新来这个论坛 – Aernor

相关问题