Search code examples
c#winformslisttext-filesstructure

C# - Unable to add elements (which is a list) in a list according to text file data


I am working on C# winForm application. In my project I need to read text file. and get the data in list.

For Test : used "listTest" list

For cmd : used "listCmd" list

My requirement is : I need Test1 and Test2 in mainList i,e "listTest". In Test1 -> cmd1, cmd2, cmd3 (which is in listCmd). And in cmd1 -> tx,rx. Same for Test2.

text file data

Test:Test1
cmd-1:5A 02 11 00 02 3A 05 ||5A 02 01 02 3A 3F  
cmd-2:5A 02 31 00 02 3E 00 80||5A 02 01 02 3E 43  
cmd-3:5A 02 21 00 02 00||5A 02 01 02 0B 10     
Test:Test2
cmd-1:5A 02 11 00 02 3A 05 00 14 00 42||5A 02 01 02 3A 3F  
cmd-2:5A 02 31 00 02 3E 00 00 02 00 80||5A 02 01 02 3E 43  

My Problem : I tried following code, in that, I am getting all 5 cmd values for Test1. And same for Test2. I want only 3 cmd in Test1 and 2 cmd in Test2. .

In class-1:

private void readCmd()
    {
        string cmdfile = System.IO.Directory.GetCurrentDirectory()  + "\\Cmd.txt";   
        try
        {
            string[] lines = File.ReadAllLines(cmdfile); 
            string[] splitLine;
            string[] txRx;

            cls_test testnum = new cls_test();
            str_listcmd listcmd = new str_listcmd();

            foreach (string line in lines)
            { 
                if (line.Contains("Test"))
                {
                    splitLine = line.Split(':');
                    txRx = splitLine.Skip(1).ToArray();   
                    uc.listTest.Add(testnum); 
                }

                if (line.Contains("cmd"))
                {
                    string[] delimiter = { ":", "||" };
                    txRx = line.Split(delimiter, StringSplitOptions.None);
                    listcmd.cmdNo = txRx[0];
                    listcmd.txCmd = txRx[1];
                    listcmd.rxCmd = txRx[2];
                    testnum.listCmd.Add(listcmd);
                }  
            }

            for (int j = 0; j < uc.listTest.Count; j++)
            {
                for (int i = 0; i < uc.listTest[j].listCmd.Count; i++)
                {
                string tx = uc.listTest[j].listCmd[i].txCmd;
                string rx = uc.listTest[j].listCmd[i].rxCmd;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

In Class-2 , main list is declared

 public List<cls_test> listTest = new List<cls_test>();  //main list


 struct str_listcmd
 {
   public string cmdNo { get; set; }
   public string txCmd { get; set; }
   public string rxCmd { get; set; }
 }
 class cls_test
 {
    public List<str_listcmd> listCmd = new List<str_listcmd>();
 }


 private void button1_Click(object sender, EventArgs e)
 {
            var a = listTest;
 } 

I am unable to find out what I am missing. Please help me to resolve this issue. Thanks in advance.


Solution

  • In fact you can refactor your code to make it more structured which decreases the potential bugs and increase testability. In order to solve your current problem, just replace code as follows;

    // ...
    // ...
    cls_test testnum = null; // <= Leave as NULL at the beginning
    str_listcmd listcmd = new str_listcmd();
    
    foreach (string line in lines)
    {
        if (line.Contains("Test"))
        {
            testnum = new cls_test(); // <= Init object for each Test line
            // ...
            // ...