Question

is there any way to clean this up? I have managed to solve when either media, advertiser, product, etc... are null. Short of writing 15+ if statements, is there a way to insert values when there are values, ignore when there are no values? For instance, if estimate != null, but estimate["EndDate"] == null, I still want to capture estimate["StartDate"]...

// declare the buy type container
Dictionary<string, object> buyType = new Dictionary<string, object>();

// get the buy type objects
Dictionary<string, object> media = (Dictionary<string, object>)detail["Media"];
Dictionary<string, object> advertiser = (Dictionary<string, object>)detail["Advertiser"];
Dictionary<string, object> product = (Dictionary<string, object>)detail["Product"];
Dictionary<string, object> estimate = (Dictionary<string, object>)detail["Estimate"];

buyType.Add("CampaignId", campaignContainer["id"].ToString());
buyType.Add("PublicId", campaignContainer["PublicId"].ToString());
buyType.Add("BuyType", detail["BuyType"].ToString());
if (media != null)
{
    buyType.Add("MediaCode", media["Code"].ToString());
    buyType.Add("MediaName", media["Name"].ToString());
}
if (advertiser != null)
{
    buyType.Add("AdvertiserCode", advertiser["Code"].ToString());
    buyType.Add("AdvertiserName", advertiser["Name"].ToString());
    buyType.Add("AdvertiserBusinessKey", advertiser["BusinessKey"].ToString());
}
if (product != null)
{
    buyType.Add("ProductCode", product["Code"].ToString());
    buyType.Add("ProductName", product["Name"].ToString());
    buyType.Add("ProductBusinessKey", product["BusinessKey"].ToString());
}
if (estimate != null)
{
    buyType.Add("EstimateCode", estimate["Code"].ToString());
    buyType.Add("EstimateName", estimate["Name"].ToString());
    buyType.Add("EstimateBusinessKey", estimate["BusinessKey"].ToString());
    buyType.Add("EstimateStartDt", estimate["StartDate"].ToString());
    buyType.Add("EstimateEndDt", estimate["EndDate"].ToString());
}
buyType.Add("CreatedBy", System.Reflection.Assembly.GetExecutingAssembly().FullName.ToString());
buyType.Add("CreatedDt", DateTime.Now.ToString());
Was it helpful?

Solution

The short answer is no, you cannot get around the null check.

You do have a couple options:

  1. Wrap the whole thing in a try catch (or even each individual Add call). This will prevent things from blowing up, but if you wrap the whole thing, any lines after the throwing line won't run, and if you do each line you might as well write the if statement.

  2. Use the null coalescing operator to put defalut values in the case of a null:

    buyType.Add("EstimateEndDt", estimate["EndDate"] ?? String.Empty);
    

    You will need a string cast or something since .ToString() before the ?? check would still throw.

  3. Use a ternary operator instead of the if:

     buyType.Add("EstimateEndDt", estimate["EndDate"] == null ? String.Empty : estimate["EndDate"].ToString());
    

This is the same as an if statement, but is a little shorter!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top