none
resize array of records? Or is there a better solution? RRS feed

  • Question

  • I need to read a list of records from a text file. Each record is a person, with regular fields of ID, name, email'phone.

    A list is probably not a good solution, since the order should be maintained.

    So I was thinking of an array, but I have no idea how many records are there.

    What is the recommended practice? 

    To read one record at a time and resize the array accordingly?

    Thanks 

    Yigal

    Wednesday, February 27, 2019 2:48 PM

Answers

All replies

  • Hi YigalB,

    I'm afraid your description is too general, and missing environmental factors.
    Visual Studio 2017?  VB.NET?  DataGridView?  Excel 2016, Access 2020? Could you provide more detailed information?

    Regards,


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Wednesday, February 27, 2019 11:19 PM
  • Sorry - Visual Studio 2017, C#
    Thursday, February 28, 2019 4:36 AM
  • Thanks - but still vague. I cannot say more.

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, February 28, 2019 4:41 AM
  • Hi Yigal,

    Welcome to the MSDN forum.

    Per the description, this issue is about the C# development and our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, February 28, 2019 6:40 AM
  • I think that a list like ‘List<Person>’ is suitable. You can append the records using ‘list.Add(new Person(…))’. It preserves the order. You can also use it like an array: var person = list[i]’. Check other possibilities of lists:

     

    • Marked as answer by YigalB Thursday, February 28, 2019 8:34 AM
    Thursday, February 28, 2019 6:42 AM
  • Hi YigalB,

    You could try to convert txt file to a table.

    Here is a simple example for your reference. You could invoke it with txt file path and columns(ID, name, email, phone).

      public static void Main()
            {
                var table = ConvertToDataTable("1.txt", 4);        
            }
            public static System.Data.DataTable ConvertToDataTable(string filePath, int numberOfColumns)
            {
                System.Data.DataTable tbl = new System.Data.DataTable();
    
                string[] lines = File.ReadAllLines(filePath);
    
                string[] Column = lines[0].Split(' ');
                for (int col = 0; col < numberOfColumns; col++)
                    tbl.Columns.Add(Column[col]);
    
                for (int i = 1; i < lines.Length; i++)
                {
                    var cols = lines[i].Split(' ');
    
                    DataRow dr = tbl.NewRow();
                    for (int cIndex = 0; cIndex < numberOfColumns; cIndex++)
                    {
                        dr[cIndex] = cols[cIndex];
                    }
    
                    tbl.Rows.Add(dr);
                }
                return tbl;
            }

    My text file. It has four columns with 3 records.

    My table result.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 28, 2019 7:06 AM
    Moderator
  • I think that a list like ‘List<Person>’ is suitable. You can append the records using ‘list.Add(new Person(…))’. It preserves the order. You can also use it like an array: var person = list[i]’. Check other possibilities of lists:

     


    I was under the impression that lists are un-ordered by nature, but I tested and you have a good point. 

    Thank you!

    Yigal 

    • Marked as answer by YigalB Monday, March 4, 2019 12:31 PM
    Thursday, February 28, 2019 8:36 AM