Three-part question.
I posted a question yesterday asking about nested classes and such (great help, by the way). However, now I am continuing with this same program, which utilizes an MDI to keep track of multiple generic stores and their respective inventories, to be instantiated whenever opened from a custom .inv file. To this end, both the Store class and Record class are serializable to save to said custom file.
I don't know what it is, but I just don't understand Serialization. I understand the concept well enough, just not the practice, so I ask for a bit of patience.
Part 1) I've created a saveFileDialog in my original form, designed to save whatever files I put into it with a .inv file extension. Now, the question is how exactly do I pass it an instantiated class to save in such a way that allows me to open the file (below) later?
Part 2) For the openFileDialog, two parts.
Firstly, after saving it above, how do I read the information in? I mean, I thought about using a text document with a renamed file extension to store all values just so I could read in the store name and the records one at a time, but since I don't know serialization as well as I need to, I don't know if that's a good idea or not.
And secondly, how would I go about instantiating the instance I'm trying to open? Is it as simple as creating one instance of the class and every time I open a new document I clear everything and just add the values from the opened file, or is it more complex than that?
The code for the classes is below, since this'll give you an idea at what I'm intending to save. If you need any other code, let me know.
using System.Text;
using System.Threading.Tasks;
namespace Inventory
{
[Serializable]
class Store
{
public Store() { }
public Store(string name) { }
public string name { get; set; }
public List<Record> Records { get; private set; }
}
[Serializable]
class Record
{
public Record() { }
public Record(int ID, int Quantity, double Price, string Name) { }
public int id { get; set; }
public int quantity { get; set; }
public double price { get; set; }
public string name { get; set; }
}
}
You can use a simple helper to serialize/deserialize strings (I've built one for you as an example):
Now in you MDI app you can manage store XML using the helper in this way:
Now you can read/write xml from your file and handle it. I hope this can help.