Click here to Skip to main content
14,774,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to save the updated values(save and reload) to a text file
When user closes, re-opens, we should see the where it left off.

1)In this code, I have a button that changes the flag values to true,
BUT, the main class does not see the changed flag values.

Still, the old values are written in txt file.

2) my "f2" object is unused, is that fine?

[JsonObject(MemberSerialization.OptIn)]
   class Class1
   {
       public bool flag;
       public bool flag2;
       public Class1()
       {
           flag = false;
           flag2 = false;
       }
   }


[Serializable]
    [JsonObject(MemberSerialization.OptIn)]
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            
        }
        Class1 c1 = new Class1();

        private void Button1_Click(object sender, EventArgs e)
        {
            c1.flag = true;
            c1.flag2 = true;
            Console.WriteLine("Flag changed: " + c1.flag);
            Console.WriteLine("Flag2 changed: " + c1.flag2);
        }


static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            Class1 c1 = new Class1();
           
            Console.WriteLine("Flag changed: " + c1.flag);
            Console.WriteLine("Flag2 changed: " + c1.flag2);

            string json = JsonConvert.SerializeObject(c1);
          
            Console.WriteLine(json);
            File.WriteAllText("path.txt", json);
            
            string json2 = File.ReadAllText("path.txt");
            Form1 f2 = JsonConvert.DeserializeObject<Form1>(json2);
            Console.WriteLine(json2);

        }


What I have tried:

[JsonObject(MemberSerialization.OptIn)] is suggested but it didn't work
Posted
Updated 18-Jul-19 0:01am
v2
Comments
Dominic Burford 18-Jul-19 4:50am
   
What are the values before you write them to the file? Are they correct or incorrect at that point?
Danny96 18-Jul-19 4:57am
   
As you can see in the Class1, they are set to false, when I press the button they are set to true, but main function can not see the changes, writes false in txt file.

1 solution

JSON does not serialize fields - it only serialises properties, so MemberSerialization.OptIn will make no difference.
Change your class:
public class Class1
    {
    public bool flag { get; set; }
    public bool flag2 { get; set; }
    public Class1()
        {
        flag = false;
        flag2 = false;
        }
    }
And discard the attribute - you'll get a slightly better result (but not much better, since you always serialize a new Class1 Instance so it will always save false values: you need to serialize the instance inside your Form, not outside it).
   
Comments
Danny96 18-Jul-19 6:48am
   
yes it worked, I did it like
//////////////////////////////////////////////
c1.flag = true;
string json = JsonConvert.SerializeObject(c1.flag);
File.WriteAllText("path.txt", json);

c1.flag2 = true;
tring json2 = JsonConvert.SerializeObject(c1.flag2);
File.WriteAllText("path.txt", json2);
//////////////////////////////////////////////
Whenever I make the flag true, I serialize it, but in the txt file it shows only 1 true, there must be 2 trues like {true,true}, for each flag
OriginalGriff 18-Jul-19 7:13am
   
What did you expect it to hold, when you only pass it a single bool value to serialize?
You don't serialize to JSON individual values, you serialize the whole class instance!
Try:

c1.Flag = true;
c1.flag2 = true;
string json = JsonConvert.SerializeObject(c1);


and see what happens!
Danny96 18-Jul-19 7:27am
   
thank you so much :) !
OriginalGriff 18-Jul-19 7:30am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900