[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

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!
0
dyarosh
Asked:
dyarosh
1 Solution
 
anarki_jimbelCommented:
Try:

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


I.e., remove the 'ILoadServerList ' type indication from "this" call.
0
 
dyaroshAuthor Commented:
I tried everything but that.  Thank you!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now