Im getting the following error:
Cannot use local variable ‘dob’ before it is declared
Here is my implementation
public class Person
{
...
public string dob { get; set; }
...
public int getAge()
{
DateTime origin = DateTime.Parse(dob);
return DateTime.Today.Year - origin.Year;
}
public string getFormattedDoB()
{
DateTime origin = DateTime.Parse(dob);
string dob = origin.ToString("d");
return dob;
}
}
I am not sure what to make of this because it is complaining about it’s use of dob in getFormattedDoB() but not in getAge() which comes before it. If anyone could shed some light on this that would be great
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:
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.