Question How to populate a nested list of objects?

user4999

New member
Joined
Jul 28, 2021
Messages
3
Programming Experience
Beginner
I have a class as shown below:

C#:
public class Par
{
    public string id { get { return id; } set { id = value; } }
    public Int32 num { get { return num; } set { num = value; } }
}

Now I also created a class named "Msg" which has a nested list of Par objects

C#:
public class Msg
{
    private Int32 mid;
    private Int32 mnum;
    public List<Par> pList; //list of Par objects

    //constructor of Msg
    public Msg(Int32 mid, Int32 mnum)
    {
        this.mid = mid;
        this.mnum = mnum;
        this.pList = new List<Par>();
    }

    public void add_ParObject_To_pList(Par obj)
    {
        pList.Add(obj);
    }
}

In the main method, I am looping through each row of my text file and extracting its contents using a foreach loop
C#:
foreach (string line in lines)
{    //I have instantiated a list of Msg objects named "MList"
    List<Msg> MList = new List<Msg>();

    //my code to extract data from each line -> mid,mnum

    Par pobj = new Par(id, num); //instantiating a Par object

    //code to check within MList if there exists a Msg object with extracted mid and mnum values from file. If not, then new Msg object must be added to this list passing in this mid and mnum.
    MList.Add(new Msg(mid, mnum));
}

Question :
How do I call and populate the "pList" (list of Par objects containing id and num), which belongs to the Msg object in this foreach loop?

Sample data of text file

mid mnum id num
1 10 1000 200
1 10 2000 201
2 20 1000 101
2 20 2000 102

the code should arrange the data in the msgList as below:

msglist[0] -> 1 10 , pList[0]: 1000 200,
pList[1]: 2000 201

msglist[1] -> 2 20, pList[0]: 1000 101,
pList[1]: 2000 102

I would greatly appreciate any suggestions/input. Thanks in advance.
 
Last edited by a moderator:
Your classes are both wrong. The Par class should be like this:
C#:
public class Par
{
    public string Id { get; set; }
    public int Num { get; set; }
}
and your Msg class should be like this:
C#:
 public class Msg
 {
     private int id;
     private int num;

     public List<Par> Pars => new List<Par>();

     public Msg(int id, int num)
     {
         this.id = id;
         this.num = num;
     }
 }
I can't say that I'm a fan of your abbreviated names either - Message is better than Msg, etc - but the main issue is the structure of your classes. You can now do this:
C#:
var messages = new List<Msg>();
var message = new Msg(1, 2);

message.Pars.Add(new Par {Id = 3, Num = 4});
messages.Add(message);
That's how objects with collections are handled through the .NET Framework.

As for your building a list of Msg from that data, how about you start by thinking about the steps involved? I have little doubt that you could do it with ease if you had to do it manually, so what are the steps you would perform as part of that manual process? No surprise, those are the same steps that you need to perform in code, so work out the steps first and then think about the code required to implement those steps. That's how you address programming problems, not thinking purely about the result and assume the code in between should just materialise for no reason. Once you have an algorithm worked out, any time you encounter an issue implementing a specific step, you can ask us specifically about that step, not about the entire process.
 

Latest posts

Back
Top Bottom