Which code is the suggested way?

Which code is the suggested way?

Old forum URL: forums.lhotka.net/forums/t/8111.aspx


FCazabon posted on Tuesday, December 08, 2009

If this is too much of a newbie question, let me know and I'll go ask elsewhere.

I'm going through the book and was looking at the code in the Project.cs file and trying to do a simple system of my own.

The book suggests code like this:

private static PropertyInfo SurnameProperty = RegisterProperty(new PropertyInfo("Surname", "Surname"));
public string Surname
{
get { return GetProperty(SurnameProperty); }
set { SetProperty(SurnameProperty, value); }
}


But the code in the sample application is like this:

    private static PropertyInfo NameProperty = 

RegisterProperty(p=>p.Name, "Project name");
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}


Is this the better/newer way to do this now? Can someone explain to me the difference between the two?

Thanks

richardb replied on Tuesday, December 08, 2009

The second method removes the need for hard coding the property name as a string, and should therefore improve maintenenance.

I think this came out just after the book went to print.

FCazabon replied on Tuesday, December 08, 2009

Thanks Richard

Copyright (c) Marimer LLC