Mixed Type List To String

Ant

New member
Joined
May 26, 2025
Messages
2
Location
S. E.. Michigan
Programming Experience
Beginner
I created a class with int, float, and string types. As I parse a file I create a dynamic List of Node Objects.

C#:
 List<Node> myNode = new List<Node>(); // my class individual node data

so I endup with myNode[0] thru myNode[x]

With the list complete I want to export to a simple csv file. But I'm having trouble getting the Object into string form to utimately export to a csv file. I tried a couple string methods, none seems to create a valid string.

C#:
string csvOut = tpNode[0].ToString();

            var stringList = tpNode.OfType<string>();

Is there a method that would work on a single Object?
 
Recall that every object in C# has a ToString() method on it. It's just a matter of overriding the compiler generated one and implementing your own, or taking advantage of it.

The way I would do it is similar to this pseudocode:
C#:
class abstract Node
{
}

class abstract Node<T> : Node
{
    protected T _value;

    // override the default implementation
    public override ToString()
    {
        string s = _value.ToString();
        bool needsToBeProtected = (s.Contains(',') || s.Contains(' ') || s.Contains('"')
        return needsToBeProtected ? ProtectString(s) : s;
    }

    string ProtectString(s)
    {
        // if there are double quotes within the string escape them.
        // return the string in quotes.
    }
}

class IntNode : Node<int>
{
}

class FloatNode : Node<float>
{
}

class StringNode : Node<string>
{
}




// build the list while parsing
List<Node> nodes = new();
while (get next token)
{
    switch (token.Type)
    {
        Token.Int:
            nodes.Add(new IntNode(token.Value));
            break;
        Token.Float:
            nodes.Add(new FloatNode(token.Value));
            break;
        Token.String:
            nodes.Add(new StringNode(token.Value));
            break;
    }
}

// get a comma separated list from the
string commaSeparatedRow =string.Join(", ", nodes);
 
I ended up writing out piece meal what I hoped a single method would do. It works, but it is more hard coded, specific to myClass(), not dynamic.

C#:
            while ( n < tpNode.Count)
            {
                cvsOut = tpNode[n].sNum + ","
                            + tpNode[n].Motion.ToString() + ","
                            + tpNode[n].Speed.ToString() + ","
                            + tpNode[n].tSlice.ToString() + ","
                            + tpNode[n].X1.ToString() + ","
                            + tpNode[n].Y1.ToString() + ","
                            + tpNode[n].Z1.ToString() + ","
                            + tpNode[n].Dist1.ToString() + ","
                            + tpNode[n].X2.ToString() + ","
                            + tpNode[n].Y2.ToString() + ","
                            + tpNode[n].Z2.ToString() + ","
                            + tpNode[n].Dist2.ToString()
                            ;
                n++;
                tw.WriteLine(cvsOut);
            }
            tw.Close();
 
Last edited by a moderator:
Oh! It was the entire node's various properties that are different types that you wanted saved out in CSV rows, not individual nodes. Your thread title made it sound like the Nodes in the list that were different types.

In that case consider this:
C#:
class Node
{
    :
    //
    // encapsulate information about the fields and their order within the Node
    //

    public sting GetCsvHeaderString()
        => "sNum,Motion,Speed,tSlice,X1,Y1,Z1,Dist1,X2,Y2,Z2,Dist2";

    public string GetCsvRowString()
        => $"{sNum},{Motion},{Speed},{tSlice},{X1},{Y1},{Z1},{Dist1},{X2},{Y2},{Z2},{Dist2}";
    :
}

:
using (var tw = new StreamWriter("MyCsvFile")
{
    tw.WriteLine(tpNode[0].GetCsvHeaderString());
    foreach(var node in tpNode)
        tw.WriteLine(tpNode.GetCsvRowString());
}
 
Last edited:
Back
Top Bottom