1

This is for c#

I'm an old dinosaur, writing 360 assembler since the 70's, trying to write stuff for the PC. Along the way I am replacing my old write it myself thinking with use the existing infrastructure.

Here is what I have now. Two objects, System and Planet. A field in System has a pointer to the next System, there is also a second chain of Systems that meet current selection criteria. Also System has a pointer to Planet and Planet has a pointer to the next Planet. Planet also has a chain of all planets.

Now the questions. Should I use lists and have C# handle all the linking etc. I'm fairly sure 1 object instance can be in multiple lists, so I can have 1 list of all systems and a second list of selected systems. Plus have a list of Planets in the system and another list of all Planets.

I also want to save this mess to disk. I've spent some time looking at serialization and it appears to be great at saving all the instances in a list, but things break down when you want to serialize multiple classes. Am I missing something basic, just a yes will send me back to looking, or do I have to roll my own?

I don't want code examples, just a gentle puch in the direction I should be looking at.

Peter
  • 83
  • 8
  • linked list pretty much only exists in academic books and interview nowadays. as for serialization you can find exactly what you need by asking google with keyword `c# serialization` – Steve Sep 05 '18 at 21:38
  • 1
    We usually call pointers references in C#. If your system to system and planet to planet relationships only have parent and child, a list would do, if you need more complex relationships you could easily spin up your own class that has a list of parents and children. as for linked lists, as @Steve said, its probably not needed, through there is Base Class Library Linked lists classes if you need them – TheGeneral Sep 05 '18 at 21:40
  • I think you are on the right track, just keep plumbing out your classes with what you need, serialization in C# is pretty easy to get working, even for complex objects and lists. when you are stuck with a specific problem feel free to come back and show us – TheGeneral Sep 05 '18 at 21:44
  • I've used linked lists in several projects. There is definitely a place for them. I completely disagree that their only use is academic. If most of your list modifications are adding/removing values that aren't at the end of the list and you don't need random access, linked lists are great. – itsme86 Sep 05 '18 at 22:20
  • Thanks for all the help. I went with Lists. It's easier to use the tools provided rather than force a solution. I also have serialization working. It took me a while and it was a lot harder because I did not understand it all. I night try to write a how I did it since I could find nothing on the net that matched my situation, that I could understand. Now it is all working except one point I will raise in a new topic. – Peter Sep 11 '18 at 00:13

3 Answers3

0

I would simply create two classes, one being the System with a List<Planet> containing all its planets and the other one being the Planet, containing a reference to his system (if one is required). The systems are themselves saved in a List<System>. Like the planets they could hold a reference to their parent so they have access to the list, but if they don't need to, its fine.

Saving this stuff should be three lines of code with a serializing system of your choice, either in text or binary (Json.Net, the Xml stuff .Net provides, yaml, binary formatter...).

Linked lists are not worth the implementation, they aren't as useful as dynamic arrays (like the List<T> in System.Collections.Generic or the Vector<T> in C++) which resize themselves when needed, and they aren't that easy to keep track of. They definetly have applications but this is not one of them IMO.

Max Play
  • 3,717
  • 1
  • 22
  • 39
0

From the sound of it, I will create class of System, Planet with one to many reference of planets in System (List here). In order to avoid strong coupling between System and Planet, One can look at Chain of Responsibility pattern.

Saving this data to database one can serialise using Json.Net (newtonsoft). SQL server supports directly putting json array.

Pseudo code:

class Planet {
  public Planet(System system) {System = system;}
  public System System {get; private set;} // singleton
}

class System {

  public Planet Planet {get; set;}

  // list of planets
  private List<Planet> planets = new List<Planet>();
  public List<Planet> Planets { get {return planets; } }
}
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

Should I use linked list or list...

The answer depends on what your object represents and how you are going to use it. For example, if I was representing houses, and the people who live at each house; then I might choose to have a collection of House objects. I'm using collection as a generic term there: specifically, I would probably use List<T> from the System.Collections.Generic namespace (where T can represent any type, so it would be a List<House> in this case), unless I needed something more specific like a Stack<T>, Queue<T>, Dictionary<T,U>, etc, etc.

Notice how in this approach, each House doesn't know which house is next, because the whole concept of 'next' relates to the collection of houses: each individual house doesn't need to know where it is in the collection - that's the responsibility of the collection. This is a design principle called "separation of concerns".

For example, if I wanted to create a different collection of House objects (e.g. the ones with red front doors), I could do so by creating a new collection, referring to the same House objects; whereas with the approach mentioned of an object having a reference to the next one, I would have to create a different House object because the next value would be different in those two collections.

Using List<T> allows you to focus on writing your classes, instead of having to write the implementation of the collection.

There are also performance reasons against using linked lists unless you only plan to access the data in sequential order.

Each House has-a collection of people. So I might put a property on House called People, of type List<Person>. And if I needed to get to the house that the person was associated with, I could have a property on Person called House, of type House.

I hope this structure of Houses and People corresponds to your scenario with Systems and Planets.

Maybe also worth looking at When should I use a List vs a LinkedList

...and how do I serialize it.

Plenty on the internet, try these...

How to Serialize List<T>?

https://www.thomaslevesque.com/2009/06/12/c-parentchild-relationship-and-xml-serialization/

Hope this helps to get you started.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36