Property. What am I missing?

S

shapper

Hello,

I am defining a property and check its value:

String _Name;
public String Name {
get { return _Name; }
set {
if (ValidationHelper.IsNotEmpty(_Name))
if (ValidationHelper.IsValidLenght(_Name, 0, 200))
_Name = value;
else
_errors.Add("Name", "The length shouldn't exceed 200
characters");
else
_errors.Add("Name", "The name is required");
}
}

On my code I am using:
MyInstance.Name = "Test".

The error "The name is required" is always fired and when I debugged
it the _Name is null.

What am I missing?

Thanks,
Miguel
 
M

miher

Hi,

I think You should use 'value' instead of '_Name' in the if clauses.

If You use the code You posted the if clauses will check whether the
_original_ value satisfies Your criterias, however the newly set value
should be checked.

Hope You find this useful.
-Zsolt
 
S

shapper

Hi,

I think You should use 'value' instead of '_Name' in the if clauses.

If You use the code You posted the if clauses will check whether the
_original_ value satisfies Your criterias, however the newly set value
should be checked.

Hope You find this useful.
-Zsolt

It was exactly that.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top