MailChimp.NET icon indicating copy to clipboard operation
MailChimp.NET copied to clipboard

Please create default instances for objects

Open skironDotNet opened this issue 11 years ago • 3 comments

Scenario: I create CampaignSegmentOptions

CampaignSegmentOptions x = new CampaignSegmentOptions(); x.Conditions.Add(some obj); //here I get null object error because Conditions is null

Here is the solution: a public constructor (I will add to my fork)

/// <summary>
/// A list of criteria on which to segment a list
/// </summary>
[DataContract]
public class CampaignSegmentOptions
{
    public CampaignSegmentOptions()
    {
        Conditions = new List<CampaignSegmentCriteria>();
    }

    /// <summary>
    /// Controls whether to use AND or OR when applying your options - expects "any" (for OR) or "all" (for AND)
    /// </summary>
    [DataMember(Name = "match")]
    public string Match { get; set; }
    /// <summary>
    /// Collection of up to 5 structs for different criteria to apply while segmenting. 
    /// Each criteria row must contain 3 keys - "field", "op", and "value" - and possibly a fourth, "extra", based on these definitions: http://apidocs.mailchimp.com/api/2.0/campaigns/segment-test.php
    /// </summary>
    [DataMember(Name = "conditions")]
    public List<CampaignSegmentCriteria> Conditions { get; set; }

}

skironDotNet avatar Apr 25 '14 20:04 skironDotNet

P.S. Would be nice to go over all object and create constructors with default instances, if of course there is no reason not to do so :)

skironDotNet avatar Apr 25 '14 20:04 skironDotNet

One reason to not do this is, if you convert the object back to json this will result into a empty array instead of null value (resulting in leaving the property out in the result json).

mdissel avatar Sep 12 '14 15:09 mdissel

so you talking optimization of the resulting JSON string, usually the focus is on optimization of hours spent on development and decreasing bug possibility, anyhow, I don't care, I wish to be removed from this topic

skironDotNet avatar Sep 12 '14 21:09 skironDotNet