Wladislaw Kusnezow
Wladislaw Kusnezow

Reputation: 103

Is it possible to save/store objects which are in an array in a file?

So i´ve got this school task where I schould write a consolecode in which I could create various products, simply by typing them in.

I have managed to use an object array to make it easier to handle the creation, editing and deleting of the products.

I wondered if it is possible to store this object array in any kind. When their is a nice way to do it, could you explain it to me?

So with the some help i figured out how to store it now :D. Big Thank you! But now my problem is: that everything goes fine except that the attribute "private double menge" does not get stores.

My Class:

[Serializable]
class Artikel
    {
        public string artikelnummer;
        public string artikelbezeichnung;
        private double Menge;
        public string Mengeneinheit;
        public double Preis;

        public void MengeAktualisieren(double abZuBuchung)
        {
            if (abZuBuchung < -1000 || abZuBuchung > 1000)
            {
                Console.WriteLine("Eingabe darf den Wert 1000 nicht ueberschreiten und den Wert -1000 nicht unterschreiten!\n");
            }
            else
            {
                Menge += abZuBuchung;
                Console.WriteLine("\n\nMenge wurde zu: " + MengeAnzeigen() + " geändert!\n");
            }
        }

        public string MengeAnzeigen()
        {
            return (Menge.ToString());
        }

        
        [JsonConstructor]
        public Artikel(string anlegen_artikelnummer, string anlegen_artikelbezeichnung, double anlegen_Menge, string anlegen_Mengeneinheit, double anlegen_Preis)
        {
            artikelnummer = anlegen_artikelnummer;
            artikelbezeichnung = anlegen_artikelbezeichnung;
            Menge = anlegen_Menge;
            Mengeneinheit = anlegen_Mengeneinheit;
            Preis = anlegen_Preis;
        }

    }

Upvotes: 0

Views: 1019

Answers (2)

Wladislaw Kusnezow
Wladislaw Kusnezow

Reputation: 103

With the Help of @Linker-SJNF I figured out how to serialize and deserialize my object array. Their was this little problem where my attribute: private double menge won´t serialize...

The answer to this was to put [JsonProperty] above the declaration of this attribute. Now everything works just fine!

Big thank you to @Linker-SJNF and @dr.null for your help! Wish you best luck!

Upvotes: 1

Srijon Chakraborty
Srijon Chakraborty

Reputation: 2164

Yes. You can do that in several ways but the most popular way is that to serialize that array and write it into a file and when you need to retrieve that array from file, than read that certain file and DE-serialized the file content and convert that content to array. One of the most popular way is to use "Newtonsoft.Json" library. Here is my code given below.Please try it and let me know it works or not.

private void ArrayToJSONString(MyClass[] items,string outpath)
{  
       string jsonData = JsonConvert.SerializeObject(items);
       var myFile = File.Create(outpath);
       myFile.Close();
       File.WriteAllText(@"" + outpath, jsonData);           
}
  private void JSONStringToArray(string filepath)
  {
      string jsonData = File.ReadAllText(filepath);             
      MyClass[] items = JsonConvert.DeserializeObject<MyClass[]>(jsondata);           
  }

Note: JsonConvert Class is in Newtonsoft.Json Library. It is free and available for both .NET and .NET Core.

Upvotes: 1

Related Questions