Local Variable Declaration Issue

后端 未结 4 1095
醉梦人生
醉梦人生 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:42

    You've declared a local variable in getFormattedDoB called dob. The compiler can't tell the difference between that and the member dob. Try adding "this" where you mean the member variable rather than the local:

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

    Better still, don't use the same name for the local variable.

    Edit: Unless you did actually intended to set the member variable and not create a new one. In which case remove the "string" as Andrew suggested.

提交回复
热议问题