0

I have code where I'm trying to set property like below.

protected ICredentials Credentials
{
    get
    {
        if (Credentials == null)
        {
            Credentials = string.IsNullOrEmpty(ApplicationId) || string.IsNullOrEmpty(Password)
                ? CredentialCache.DefaultNetworkCredentials
                : new NetworkCredential(ApplicationId, Password);
        }
        return Credentials;
    }
}

It is giving error like below.

enter image description here

"ICredentials" is an "Interface" from "System.Net". See here

Any idea how to fix it?

I checked following threads but they are not of any use.

Property or indexer cannot be assigned to -- it is read only

Property or indexer cannot be assigned to :: string2[i] = sting1[i]

cannot be assigned to -- it is read only - C#

"cannot be assigned to -- it is read only" error on ConditionExpression

Community
  • 1
  • 1
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47

1 Answers1

1

You are referring to the property itself inside the property implementation, which is wrong on its own. Besides, Proxy does not have a setter, so you cannot set it, and that is exactly what the error message is saying.

What you are probably up to can be done with a private field though:

private IWebProxy _proxy;
protected IWebProxy Proxy
{
    get
    {
        if (_proxy == null)
        {
            _proxy = HttpWebRequest.DefaultWebProxy;
            _proxy.Credentials = CredentialCache.DefaultCredentials;
        }
        return _proxy;
    }
}
Andrei
  • 55,890
  • 9
  • 87
  • 108