0

I have this to check digits:

 private bool IsDigitsOnly(string str)
    {
        foreach (char c in str)
        {
            if (c < '0' || c > '9')
                return false;
        }
        return true;
    }

This to check string len

 private bool CorrectFormat(string str)
    {
        if (str.Length == 9)
            return true;
        return false;
    }

I use this on property SET

 public string NIF
    {
        get { return nif; }
        set { if (IsDigitsOnly(nif) && CorrectFormat(nif))
                nif = value;
        }

But when I try to make an object instance I got an error "System null reference exception" when I am using 123456789 in parameter that is always true when checked by two methods before.

Some help?

André
  • 37
  • 6

1 Answers1

9

You have to use value instead of nif in the property setter:

public string NIF
{
    get { return nif; }
    set { if (IsDigitsOnly(value) && CorrectFormat(value))
            nif = value;
    }
}

You're going to assign the value to the backing field nif after the checks.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939