I would like to know which is considered more correct or better practice:
Part of the class:
public class foo
{
private Single amount;
public Single Amount
{
get
{
return amount;
}
set
{
amount = Convert.ToSingle(value);
}
}
}
or
public Single Amount {get;set;}
and converting the value in the code such as this:
foo myFoo = new foo();
myFoo.Amount = Convert.ToSingle(somevalue);
I will be getting the values (somevalue) from a database so I could easily pass all the values in as strings to the class and handle all the conversion there (or do the conversion in the data access layer). I just want to know which is better practice (or something I didn’t think of).
The second one is definitely preferable IMO:
You don’t want your class doing obscure things in the background that are not clear from the outside (like type casting).
It’s the job of the outside code to conform to the class property format, not the other way around.