Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

How can I create a constructor on a class that requires 2 parameters but defaults the second parameter if it isn't specified

I have a class that requires 2 parameters when the class is instantiated.  The way I have the constructors set up is if no parameters are passed, the parameters are defaulted and the constructor that takes 2 parameters is called.  What I want to be able to do is to have the second parameter be defaulted if it isn't specified but I haven't been able to figure out the syntax for it.  Here is what I have so far:

     public partial class DetermineEnvironment
    {
        private HttpContextBase _httpContext = null;
        private ILoadServerList _LoadServerEnvironments = null;
        private List<ServerEnvironmentData> ServerEnvironmentList { get; set; }

       public DetermineEnvironment() : this(new LoadServerEnvironments(), new HttpContextWrapper(System.Web.HttpContext.Current)) { }
        public DetermineEnvironment(ILoadServerList lsl, HttpContextBase hcb)
        {
            // Throw error if HttpContext is null)
            if (hcb == null)
                throw new ArgumentNullException("HttpContext", "HttpContext is not defined");

            // Throw error if HttpContext.Request is null)
            if (hcb.Request == null)
                throw new ArgumentNullException("HttpContext", "HttpContext.Request is not defined");

            // Throw error if LoadServerEnvironments is null
            if (lsl == null)
                throw new ArgumentNullException("ILoadServerList", "LoadServerEnvironments is not defined");

            _httpContext = hcb;
            _LoadServerEnvironments = lsl;

            ServerEnvironmentList = SetServerEnvironmentData();
        }
  }

Open in new window


I tried the following:
        public DetermineEnvironment(ILoadServerList lsl) : this(ILoadServerList lsl, new HttpContextWrapper(System.Web.HttpContext.Current)) { }

but get an error on the lsl shown in bold.  The error is ) expected.

Is there a way to do what I want?  I don't want to have to pass in the HttpContext in production but need to for Unit Testing.  Any help is greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh
dyarosh

ASKER

I tried everything but that.  Thank you!