1

EDIT: I finally managed to write into the file and solved the error, however it does not update the bools, I mean I changed the flag values while program is running, yet it does not change the values.

Main class does not see the changes of flags.

I want to save and reload the class boolean variables where it is left off, I tried to use JSON to write and read variables to txt file. However, neither the code creates a txt file nor writes in it.

(I have a button that changes the values of flags)

I am so confused and I don't know what to do. Can you please help me?

using Newtonsoft.Json; using System.Web.Script.Serialization;

EDITED CODE

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

[Serializable]
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);
  }

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

  Class1 c1 = new Class1();
  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);

B B
  • 121
  • 9
  • 2
    Why not use `JsonConvert.SerializeObject()`? You're using DeserializeObject – Artog Jul 18 '19 at 06:20
  • it firstly, serialize then deserialize so that it reads and writes to file – B B Jul 18 '19 at 06:24
  • 1
    Not a solution to your problem, but some things worth noting: The `Form1` in `Application.Run(new Form1());` isn't the same instance as `Form1 f = new Form1();` (imagine you buy two Toyota Corollas - 100% identical cars. They're still different cars. If you crash one, the other one is still OK - well, unless you crash one into the other, but that's not relevant to the analogy). Furthermore `Application.Run(...)` will block until the first `Form1` is closed. As for your problem, I'd probably advise against serialising the Form itself, but instead have a data object which you serialise. – DiplomacyNotWar Jul 18 '19 at 06:24
  • But it runs the GUI, I can not change it, what do you suggest? – B B Jul 18 '19 at 06:30
  • @BB I meant why use `new JavaScriptSerializer().Serialize(f);` over `JsonConvert.SerializeObject(f);`. I don't know if it does anything different, but it can probably be a good idea to use the same class for both serializing and deserializing – Artog Jul 18 '19 at 06:31
  • well I tried that one too "string json = JsonConvert.SerializeObject(f)" yet nothing changed – B B Jul 18 '19 at 06:34
  • Try moving `Application.Run(new Form1());` last in `Main()` and changing to `Application.Run(f);` – Artog Jul 18 '19 at 06:38
  • I did it but still gives the same error I stated above – B B Jul 18 '19 at 06:47
  • I'm of the opinion that you shouldn't serialize a `Form`. At best you're storing redundant information. See [here](https://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) for more information about your error. – DiplomacyNotWar Jul 18 '19 at 06:47
  • @John I applied your logic, I put values to another class and it worked fine!! (at least it writes into a file), but it does not update the values. My "f2"object is unused but I don't know where to put it – B B Jul 18 '19 at 07:06
  • I'd suggest that you have some logic in the `Form1_Load()` to load the file if it exists (else use defaults) and set up your form with those values / look into bindings. – DiplomacyNotWar Jul 18 '19 at 07:07
  • The variables are updated at Form class when I press the button, my JSON object fetches the other class(the one I just created to prevent Form serialization), maybe in the main, the flag changes can not be seen. – B B Jul 18 '19 at 07:09

1 Answers1

1

Your code tries to serialize to much information (it tries to serialize members of the Form class). You should call JsonConvert.SerializeObject(f), and use opt-in serialization so you can control easily which members are serialized.

misterfrb
  • 205
  • 2
  • 10
  • I used that method in my edited code, however it didn't update the values in the txt file, I think there is a communication problem. It should have written the flags' new values into txt properly – B B Jul 18 '19 at 07:24
  • Did you also use opt-in serialization and mark the members you want to serialize with a [JsonProperty] attribute ? – misterfrb Jul 19 '19 at 06:55