0
votes

I'm currently making instances of my classes from a generic csv reader(see source here). It checks what properties a class has and which order they are in, and then assigns them in that order from the csv file given.

My issue is that I have a class that needs to be able to take an object property, since it needs to be able to be different types of classes(One instance of the class may contain a Foo class instance as property value while another has a Bar class instance as that property value). The reader doesn't know a string from an object though, and I can't place the separation character within the objects place in the csv. However, all classes have a Name property(string).

Could I somehow use that as a key to search all object instances for a key match?

An example of the class that should hold another class:

public class ClassHolder : CsvableBase
{
    public ClassHolder() { }
    public ClassHolder(string name, object obj)
    {
        Name = name;
        Obj = obj;
    }

    public string Name { get; set; }
    public object Obj { get; set; }
}

public class Foo : CsvableBase
{
    public Foo() { }
    public Foo(string name, int amount)
    {
        Name = name;
        Amount = amount;
    }

    public string Name { get; set; }
    public int Amount { get; set; }
}

In the Csv file it's properties would be written like this:

Name;Obj

ClassHolderName;FooName FooAmount

So the name for ClassHolder is correct, but the Obj property is the string "FooName FooAmount"

If this is not possible I'd love suggestions for another approach, I'm new to programming, and the more ways to approach a problem I know the better.

1
"Could I somehow use that as a key to search all object instances for a key match?" -- do you mean, does .NET maintain a master list of all instances of every class? No. You'd have to roll that yourself (maybe in CsvableBase -- but mind you don't break garbage collection by keeping references to everything forever). Is CSV an absolute requrement here? Because what you want is JSON or XML.15ee8f99-57ff-4f92-890c-b56153
Well I've only been introduced to CSV as a way to store information, but I'll do some research on JSON and XML. Since this seems like it might be too much work for what I get from it. The project is mainly for me to learn even more since the course has already stopped.Jacob
Yeah, kludging CSV into an hierarchical object storage format doesn't seem like a profitable use of your time. You'll get infinitely more bang for your buck learning JSON. It's a very widely used and cool format.15ee8f99-57ff-4f92-890c-b56153
Alright, I'll consider that the answer then. Thank you very much for your time and help.Jacob

1 Answers

0
votes

I would strongly advise you to use XML or JSON instead of CSV. What you're doing is serializing something hierarchical, which is what those formats were designed for. CSV was designed for serializing flat, tabular information.

You could kludge something like CSV into something suitable, but it wouldn't really be CSV any more -- and unless that particularly appeals to you as a useless fun project (I've done a few of those), I'd just go with one of the others.