Search code examples
c#jsonconstructorlinq-to-json

Can you call a second constructor without an overload method


So I have a query. It's not really a problem as the code still works but it seems sloppy.

I need to have two constructors.

public class JsonQuery
{
    const string currentWorkSheet = "GLHTanneryData_CurrentWeek";
    const string unitTestingWorkSheet = "GLHTanneryData_UnitTesting";
    private const string authorisation = "XXXX";
    JObject jObject;

    public JsonQuery()
    {
        SmartsheetQuery smartsheetQuery = new SmartsheetQuery();
        jObject = JObject.Parse(smartsheetQuery.getJsonAsString(currentWorkSheet));
    }

    public JsonQuery(bool testing)
    {
        SmartsheetQuery smartsheetQuery = new SmartsheetQuery();
        jObject = JObject.Parse(smartsheetQuery.getJsonAsString(unitTestingWorkSheet));
    }
}

Each grabs a json file from a different location on smartsheets. So in order to call the second constructor I just feed it a boolean value of true. Thing is, and as you can see from the code, I don't actually need to use any boolean value in the method I just needed to know that it was this constructor I wanted to call.

Is there a nicer way to do this?


Solution

  • You don't really need two constructors - it looks like you would benefit from passing in your worksheet:

     public JsonQuery(string worksheet)
     {
        SmartsheetQuery smartsheetQuery = new SmartsheetQuery();
        jObject = JObject.Parse(smartsheetQuery.getJsonAsString(worksheet));    
     }
    

    Just pass in a different worksheet for testing.