I know its bad practice to put error handling in properties I just want to know where I should be putting the error handling. I know that a value in a property should never change.
I have a gridview where the user is allowed to change cell 2 to any value (char) but if the user enters something larger than a char it will populate an error, is my best bet to just check on the gridview somehow?
I seem to run into this problem a bit, when I have properties or even methods that return a type, I can’t get into error handling, without doing a try catch (or TryParse) and if its wrong returning the type but blank.
public char WeightClass
{
get
{
return Convert.ToChar(gvFighters.Rows[rowNumber].Cells[2].Value);
}
}
EDIT: if you could provide some additional readings for code practices that would also be a plus read most of code complete….
EDIT
public char FlightClassFromRow()
{
char result;
if(Char.TryParse(gvSegments.Rows[rowNumber].Cells[2].Value.ToString(),out result))
{
return result;
}
//if false, return empty char? is that the best way?
}
No need for a try catch.
But if a property is doing more than a simple return it’s better to just use a method.