0

I am looking for a way to set 'null' property values to a 'non-null' value. These properties are associated with an object and there is a list of multiple objects.

The issue i am having is with converting the 'null' value to a 'non-null' value where each property has a different type.

What i have so far is a few nested loops and conditionals in an attempt to identify the null properties and set them to non-null.

//loop through each object
for (int i = 0; i < objectList.Count; i++)
{
    //loop through each object and each field within that object
    foreach (var property in objectList[i].GetType().GetProperties())
    {
        var current_field_val = property.GetValue(objectList[i], null);

        //null validation
        if (current_field_val == null)
        {
            PropertyInfo current_field_data_type = objectList[i].GetType().GetProperty(property.Name);

            if (current_field_data_type is String)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
            }
            else if (current_field_data_type is int)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 0);
            }
            else if (current_field_data_type is double)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 1);
            }
            else if (current_field_data_type is object)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
            }
        }
    }
}

Excuse my poor indenting, VS didn't play nice when copying back and forth.

Damian
  • 2,752
  • 1
  • 29
  • 28
user3801447
  • 41
  • 3
  • 6
  • thanks for the cleanup edit @Damian! – user3801447 Feb 08 '17 at 23:10
  • And what is your question exactly? What isn't working as expected? – InBetween Feb 08 '17 at 23:11
  • 3
    as an aside, this seems like a very bad idea. also ints and doubles can't be null unless they are made nullable ints and doubles. Fundamentally you've gone wrong much earlier on in your design and this is nasty hack to try and clean it up – Keith Nicholas Feb 08 '17 at 23:14
  • Because of the way in which my JSON is getting mapped to my object, whenever there is a blank field - a null is used (which is as expected). Now, i am looking to replace all nulls with a value appropriate for the property data type (prevents casting errors) however, i can't seem to work out an effective way to 1. check the specific data type and 2. set the value without a casting error. – user3801447 Feb 08 '17 at 23:15
  • why don't you handle this in your deserialzing of json then? – Keith Nicholas Feb 08 '17 at 23:16
  • Interesting, how would one go about that? Can i overload the Deserialise method or is there a c# native way to prevent the nulls from being used. – user3801447 Feb 08 '17 at 23:17
  • ie, stick by the principle of never creating objects that aren't ready to be used. – Keith Nicholas Feb 08 '17 at 23:18
  • @KeithNicholas Thanks for the tip, in this situation i can't avoid creating the objects because the deserialising does that for me and it just happens that the properties (if nothing is returned) are set to null. – user3801447 Feb 08 '17 at 23:26
  • How can you deserialize a blank field into a valid non-null object? do the objects have some sort of default state that you expect to use? – D Stanley Feb 08 '17 at 23:30
  • Oh thats actually a good idea, maybe i can use a default value to ensure that the blank field becomes something other than null. – user3801447 Feb 08 '17 at 23:34
  • errr, most deserializers are fully customizable, but questions about that don't fir this question – Keith Nicholas Feb 09 '17 at 00:10

2 Answers2

0

If you are looking for a way to generate a default non null value for any reference type then I'm afraid you are out of luck. There is no general mechanism in the language that will give you a non null default value for any given reference type; the default value is precisely null.

If the set of types you need to handle is finite and manageable then you could probably code each specific case.

Anyhow this seems pretty weird. What are you trying to achieve exactly? There are most likely better ways to solve the problem.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • The issue is that i am using the objects (deserialised from JSON) to generate some HTML, however the HTML cant be generated if there is a null value. Because of this, I am trying to remove the nulls before generating the HTML instead of checking each individual property for nulls when generating the HTML. I had the choice between the 2 and started with this. – user3801447 Feb 08 '17 at 23:24
0

After some time and research, the best way to avoid this issue is to create constructors / default values for the object that will be created in the deserialisation process and then using the settings described in this question - Why when I deserialize with JSON.NET ignores my default value?. The default constructors will be used and null values will be ignored.

objectsList = JsonConvert.DeserializeObject<List<RootObject>>(json_string, new JsonSerializerSettings
     {
                DefaultValueHandling = DefaultValueHandling.Populate,
                NullValueHandling = NullValueHandling.Ignore
      }

 );
Community
  • 1
  • 1
user3801447
  • 41
  • 3
  • 6