Search code examples
c#json.net

Setting an Integer to something, but It won't change. Why?


Currently I have this function that loads an integer from a JSON file, and then it sets the ExeBalance to the Integer it found in the JSON file however when checking the breakpoints I see that the JSON JBalance gets retrieved correctly, but it won't change the ExeBalance integer.

It is setting it to the JSON object, but It won't change the ExeBalance value:

ExeBalance = saveDataJson.JBalance;

This is my code:

namespace Money_Simulator
{
  public class DataObject
  {
    public int JBalance { get; set; }
  }
  
  internal class DataHandler
  {
    public int ExeBalance = 0;
    
    public void AddBalance(int amt)
    {
      ExeBalance = ExeBalance + 1;
      Console.WriteLine(ExeBalance);
    }
    
    public void LoadSave()
    {
      string filePath = Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory,
        "savedata.json"
      );
      StreamReader sr = new StreamReader(filePath);

      string saveDataContent = sr.ReadToEnd();
      var saveDataJson = JsonConvert.DeserializeObject<DataObject>(
        saveDataContent
      );

      ExeBalance = saveDataJson.JBalance;
      Console.WriteLine("ExeBalance was set to this value from reading savedata.json:");
      Console.WriteLine(ExeBalance);
    }
  }
}

The contents of savedata.json are {"JBalance": 5}.


Solution

  • The problem with your code is that you don't understand how this should work. My guess is you would like to add an amount to the balance that is stored in your JSON. I've modified your code to do just that. The Load() returns the deserialized JSON object. The AddBalance then adds the amount (amt) and the Save then saves it to the JSON file. Here's the modified code:

    using Newtonsoft.Json;
    
    namespace Money_Simulator
    {
        public class DataObject
        {
            public int JBalance { get; set; }
        }
    
        internal class DataHandler
        {
            private const string FileName = @"..\..\..\savedata.json";
    
            public void AddBalance(int amt)
            {
                var data = Load();
                data.JBalance = data.JBalance + amt;
                Save(data);
            }
    
            private DataObject Load()
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName);
    
                string saveDataContent = File.ReadAllText(filePath);
                var saveDataJson = JsonConvert.DeserializeObject<DataObject>(saveDataContent);
    
                Console.WriteLine($"ExeBalance was set to this value from reading savedata.json: {saveDataJson.JBalance}");
    
                return saveDataJson;
            }
    
            private void Save(DataObject data)
            {
                var output = JsonConvert.SerializeObject(data);
    
                File.WriteAllText(FileName, output);
            }
        }
    }
    

    In your main application do the following:

    using Money_Simulator;
    
    var handler = new DataHandler();
    
    handler.AddBalance(1);
    handler.AddBalance(1);
    handler.AddBalance(1);
    handler.AddBalance(1);
    handler.AddBalance(1);
    handler.AddBalance(1);
    handler.AddBalance(1);
    

    Result is:

    ExeBalance was set to this value from reading savedata.json: 10
    ExeBalance was set to this value from reading savedata.json: 11
    ExeBalance was set to this value from reading savedata.json: 12
    ExeBalance was set to this value from reading savedata.json: 13
    ExeBalance was set to this value from reading savedata.json: 14
    ExeBalance was set to this value from reading savedata.json: 15
    ExeBalance was set to this value from reading savedata.json: 16