abhi
abhi

Reputation: 3136

How can I put validation in the getter and setter methods in C#?

In C#, I can have a property without having the need to declare a private variable. My VB6 code that looked like this

'local variable(s) to hold property value(s)
Private mvarPhoneNumber As String 'local copy
Public Property Let PhoneNumber(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.PhoneNumber = 5
    mvarPhoneNumber = vData
End Property


Public Property Get PhoneNumber() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.PhoneNumber
    PhoneNumber = mvarPhoneNumber
End Property

can now look like this.

public string PhoneNumber{get;set;}

How can I put validation in the getter and setter methods in C#? I tried adding a validation like this.

public string PhoneNumber
        {
            get
            {
                return PhoneNumber;
            }
            set
            {
                if (value.Length <= 30)
                {
                    PhoneNumber = value;
                }
                else
                {
                    PhoneNumber = "EXCEEDS LENGTH";
                }
            }
        }

The get part of this code won't compile. Do I need to revert to using a private variable?

Upvotes: 8

Views: 19414

Answers (5)

ChaosPandion
ChaosPandion

Reputation: 78262

I would do something like this as to avoid a NullReferenceException as well as shorten the overall code.

public string PhoneNumber
{
    get { return _phoneNumber; }
    set 
    {
        var v = value ?? string.Empty; 
        _phoneNumber = v.Length <= 30 ? v : "EXCEEDS LENGTH"; 
    }
}
private string _phoneNumber;

Upvotes: 2

Flinsch
Flinsch

Reputation: 4341

You do not necessarily need a local variable. Theoretically, you could implement whatever functionality you want within a get/set property. But, in your example, you have a recursive access of your get/set property what makes no sense in the way it is implemented. So, in your concrete case, you will need a local variable, that's right.

Upvotes: 2

Madhur Ahuja
Madhur Ahuja

Reputation: 22681

Look here for several many alternatives: Acessing the backing field in an auto property

But the short answer is Yes, you will have to have backing field for validations.

Upvotes: 1

Patrick Karcher
Patrick Karcher

Reputation: 23603

Yes you do. When you use the shortcut "implicit" syntax, it secretly creates a backing field called _phoneNumber for you. When you explicitly define your property, you need to make your own backing field. Right above your property definition put:

private string _phoneNumber;

and then in your property get use:

get
{
    return _phoneNumber;
}

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351516

Yes, you will have to create a backing field:

string _phoneNumber;

public string PhoneNumber
{
    get
    {
        return _phoneNumber;
    }
    set
    {
        if (value.Length <= 30)
        {
            _phoneNumber = value;
        }
        else 
        {
            _phoneNumber = "EXCEEDS LENGTH";
        }
    }
}

Keep in mind that this implementation is no different from an automatically implemented property. When you use an automatically implemented property you are simply allowing the compiler to create the backing field for you. If you want to add any custom logic to the get or set you have to create the field yourself as I have shown above.

Upvotes: 18

Related Questions