Local Variable Declaration Issue

后端 未结 4 1152
醉梦人生
醉梦人生 2021-01-21 09:26

Im getting the following error:

Cannot use local variable \'dob\' before it is declared

Here is my implementation

pu         


        
4条回答
  •  死守一世寂寞
    2021-01-21 09:54

    You have reused the variable name "dob" in getFormattedDoB as a local string, which is confusing the compiler. There are 2 possible solutions:

    1. Rename the local dob in getFormattedDoB (which you really should do because it's good practice).
    2. Use this.dob in the following line to specify the class level variable (which you should probably also do because it's also good practice:

      DateTime origin = DateTime.Parse(this.dob);

提交回复
热议问题