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

Posted on 2014-08-03
Last Modified: 2014-08-03
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!
Question by:dyarosh
    LVL 29

    Accepted Solution


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

    I.e., remove the 'ILoadServerList ' type indication from "this" call.

    Author Closing Comment

    I tried everything but that.  Thank you!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now