a basic issue in implementing validations through properties ? Please guide me.

Posted by haansi on Stack Overflow See other posts from Stack Overflow or by haansi
Published on 2010-06-08T07:27:51Z Indexed on 2010/06/08 7:32 UTC
Read the original article Hit count: 190

Filed under:
|
|
|
|

hello,

thanks for your attention and time.

I want to implement validations in settter of properties. Here is an issue where your expert help is required please.

I have idea of how I will do validations before setting value. but not getting what to do if passed value is not correct. Just not setting is not a acceptable solution as I want to return an appropriate message to user (in a label in web form). My example code is:

private int id;
public int Id {
get
{ return id; }

set
{
bool result = IsNumber(value);
if (result==false)
{
// What to do if passed data is not valid ? how to give a appropriate message to user
that what is wrong ?
}

id = value;
}
}

A thought was to use return but it is not allowed.

Throwing error looks not good as generally we avoid thorwing custom errors.

Please guide and help me.

thanks in anticipation

haansi

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET