0

I am working on a small utility to manage a few computers connected on a network. This utility will have a button "Add Computer" on the UI which allows the user to configure a new computer.

I have a class called component:

public class component
{

public string name { get; set; }
public string ip { get; set; }
public string room { get; set; }

bla bla


}

Please keep I am absolutely not a professional, so forgive me if the question sounds stupid.

The first problem I got is: How can I create multiple instances of the same class at runtime? What I figured out is using a deep clone, with serialiazation and deserialization. Is it a good approach?

Second problem. Let's say at runtime I managed to create my clones and I have an object for every computer in my network. How can I create a file containing all those objects? How can I serialize them?

I am not looking for code, but just for someone pointing me in the right direction, as I am pretty sure my approach is not exactly the best. All suggestions are welcome.

Thanks a lot!

6
  • Any particular reason you are using DeepClone over, say, just creating multiple instances? Commented Oct 22, 2016 at 17:33
  • Also, possible duplicate: stackoverflow.com/questions/15247053/serialize-multiple-objects Commented Oct 22, 2016 at 17:35
  • I think you should focus on more basic things like "how to make multiple instances of" than serializing. Pick up a tutorial on C# and do some basic assignments. Commented Oct 22, 2016 at 17:40
  • @kuhaku Dear kuhaku, I totally agree with you that what I am trying to achieve is far beyond my knowledge. However, that's the problem I am facing right now and I just would like some good advice on what to look into (a keyword, an article, a book even). I didn't ask anyone to do my homework - in fact I didn't post any code). Thanks for your help. Commented Oct 22, 2016 at 20:22
  • No need for a book, just google every question you have or anything you don't understand from the answers you got. If you find yourself struggling even with that, then I suggest to start from the basics, i.e. google "learn C#" and go through the steps. Commented Oct 22, 2016 at 20:59

2 Answers 2

0

Ok, howto create instance of my class

var myInstance = new MyClass();

Howto serialize instance of MyClass to file

    var instance= new MyClass(); // <- instance
    using (var sw = new StreamWriter("Path_to_file"))
    {
        var ser = new XmlSerializer(typeof(MyClass));
        ser.Serialize(sw, instance);
    }

Howto read instance of MyClass from file

    using (var sr = new StreamReader("path_To_file"))
    {
        var ser = new XmlSerializer(typeof(MyClass));
        var myInstance = ser.Deserialize(sr) as T; // <- instance
    }
Sign up to request clarification or add additional context in comments.

Comments

0

To store inside your program multiple instances of the same class you need an object called List (here the MSDN reference and here a fast tutorial). To store the List you have multiple choice: serialize and deserialize using xml, json, protobuf protocol and much more, it's you choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.