0

Sorry I couldn't think of a better title to describe the issue.

I use the following code to make it easier to update specific values of config properties. Note that the config properties are not just integers and there are not just 2 of them, just simplified it for this example.

public class Config {
    public int VarA { get; set; }
    public int VarB { get; set; }
}
private Config config;
private void Update(Config newValues) {
    PropertyInfo[] properties = typeof(Config).GetProperties();
    foreach (PropertyInfo property in properties) {
        object n = property.GetValue(newValues);
        property.SetValue(config, n ?? property.GetValue(config));
    }
}

The Update method checks the properties of newValues and will update the properties of config if a value is defined.

I initialise config with values like so (just an example):

config = new Config() { VarA = 1, VarB = 2 };
Debug.WriteLine(config.VarA + " : " + config.VarB); // 1 : 2

Then if I only want to update VarA to a value of 0 and don't touch VarB, I do this:

Update(new Config() { VarA = 0 });
Debug.WriteLine(config.VarA + " : " + config.VarB); // 0 : 0

But this results in VarB also being set to 0 because newValues didn't have a value assigned for it and null value as int is 0 because int is non-nullable. How would I make VarB remain as value of 2 when it's not defined in newValues?

Sakuya
  • 660
  • 5
  • 23
  • Why do you **create** a new `Config` and all you want to do is to **modify** it? Why not just use `Update("VarA", 1)` on an **existing** `Config`? – MakePeaceGreatAgain Feb 28 '19 at 09:49
  • 3
    "But this results in VarB also being set to 0 because newValues didn't have a value assigned for it and null value as int is 0 because int is non-nullable." Well, sort of. It's not that "null value as int is 0" so much as "the default value for int is 0". – Jon Skeet Feb 28 '19 at 09:50
  • 3
    But fundamentally, if you want to use "null" for "hasn't been set" then your properties should all be reference types or nullable value types. – Jon Skeet Feb 28 '19 at 09:51
  • @HimBromBeere It's just a simplified example. These properties are first populated from different json files, then I compare one with the other to check for changed values and update them, but it's variable whether a specific property is defined/changed. – Sakuya Feb 28 '19 at 10:02

2 Answers2

1

Thanks Jon Skeet, I wasn't aware you could make value type nullable, so the following works:

public class Config {
    public int? VarA { get; set; }
    public int? VarB { get; set; }
}

I'm aware that the other answer can work in some cases, but I'm populating these config properties from different json files and there are many properties, so I wouldn't want to type them all out manually.

Sakuya
  • 660
  • 5
  • 23
0

In the update procedure for your (Config newValues) parameter give existing modified Config parameter instead of creating new config object.

Creation of new Config object resets all existing values.

config = new Config() { VarA = 1, VarB = 2 }; 
Debug.WriteLine(config.VarA + " : " + config.VarB);

config.VarA = 0;
Update(config);
Debug.WriteLine(config.VarA + " : " + config.VarB);
Serhat Oz
  • 788
  • 8
  • 12