I have a class as shown below:
Now I also created a class named "Msg" which has a nested list of Par objects
In the main method, I am looping through each row of my text file and extracting its contents using a foreach loop
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.
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: