PositiveGuy
PositiveGuy

Reputation: 47743

Help with Singleton - Setting Auto Properties?

I'm trying to set my Auto Properties but they are non-static and so I get the error "Cannot access non-static property in static context" when trying to set the properties Credentials, Certificate, and UrlEndPoint.

public class PayPalProfile
{
    #region Fields

    static PayPalProfile _instance;

    #endregion

    #region Constructors

    PayPalProfile()
    {
        // is only called if a new instance is created
        SetProfileState();
    }

    #endregion

    #region Properties

    public static PayPalProfile CurrentProfile
    {
        get
        {
            if (_instance == null)
                _instance = new PayPalProfile();

            return _instance;
        }
    }

    public CustomSecurityHeaderType Credentials { get; private set; }

    public X509Certificate2 Certificate { get; private set; }

    public string UrlEndPoint { get; private set;}

    #endregion

    #region Methods

    private static void SetProfileState()
    {
        // Set the profile state
        SetApiCredentials();
        SetPayPalX509Certificate();
    }

    private static void SetApiCredentials()
    {
        Credentials = new CustomSecurityHeaderType
                           {
                                Credentials =
                                {
                                    Username = PayPalConfig.CurrentConfiguration.ApiUserName,
                                    Password = PayPalConfig.CurrentConfiguration.ApiPassword
                                }
                           };

        UrlEndPoint = PayPalConfig.CurrentConfiguration.ExpressCheckoutSoapApiEndPoint;
    }


    private static void SetPayPalX509Certificate()
    {
        PayPalCerfiticate paypalCertificate = new PayPalCerfiticate();

        Certificate = paypalCertificate.PayPalX509Certificate;
    }

    #endregion
}

Upvotes: 1

Views: 282

Answers (2)

Simon Fox
Simon Fox

Reputation: 10561

There is no need for SetProfileState, SetApiCredentials and SetPayPalX509Certificate to be static.

SetApiCredentials and SetPayPalX509Certificate are setting values for non static properties and so an instance is required. By removing the static modifiers from the above mentioned methods the properties will be set on the instance being constructed when SetProfileState is called.

Upvotes: 2

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24098

This means that you have a static method where you are trying to assign instance properties. As there is no instance available in static methods/properties, the error is given.

The example:

public class Test {
  public int InstanceProperty { get; set; }
  public static void StaticMethod() {
    InstanceProperty = 55; // ERROR HERE
  }
}

Instead bothe should either be in static or instance context:

public class Test {
  public static int StaticProperty { get; set; }
  public static void StaticMethod() {
    StaticProperty = 55; // Ok
  }
}

public class Test {
  public int InstanceProperty { get; set; }
  public void InstanceMethod() {
    InstanceProperty = 55; // Ok
  }
}

Upvotes: 0

Related Questions