everyone, this is my first time posting! :D
I have a question regarding creating some sort of a tree view in a console app in C#. I have a text file that is filled with a number of rows, that have their IDs, texts and ParentIDs.
My assignment is to order every row, so the one with the ParentID == 0 is the first, and then the rest will follow, BUT I have to use a recursive method for the order.
So far I have managed to parse the text file, place it into a class and place the classes in a list, but I have not been successful with the recursive part. Here is my unsuccessful attempt:
static void Main(string[] args)
{
string[] lines = File.ReadAllLines(@"C:\Users\text.txt");
List<Object> objects = new List<Object>();
foreach (string line in lines)
{
string[] col = line.Split(",");
objects.Add(new Object(int.Parse(col[0]), col[1], int.Parse(col[2])));
}
Recursive(objects, objects.Count-1);
foreach (var obj in objects)
{
Console.WriteLine("{0},{1},{2}", obj.ID, obj.name, obj.ParentID);
}
}
public static object Recursive(List<Object> objects, int a)
{
if (objects[a].ParentID == 0)
{
return objects[a];
}
else
{
return Recursive(objects, a-1);
}
}
}
public class Object
{
public int ID;
public string name;
public int ParentID;
public Object(int ID, string name, int ParentID)
{
this.ID = ID;
this.name = name;
this.ParentID = ParentID;
}
}
If anybody has any idea how to solve this, I would really appreciate it. I understand the concept of the recursive function, but I just can't seem to get around the logic of this particular assignment.
Here is a sample of the text file:
1,text,0
2,text,1
3,text,2
4,text,5
5,text,1
6,text,4