Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to work around this method but the output I get it's not what I need.

When the program run at main I get to insert data into the array and after that should be able to create a table adding the data within. The problem is get a really bad formatation. Does anyone could tell what's the problem of the LISTDATA<t> method? I haven't got this one.

Sorry if I've forgotten any useful info and thanks for looking.

What I have tried:

C#
public class Program
{
    private static void Main()
    {
        var client = new string[4, 7];
        InsertData<clientheader>(client);

        Console.Clear();
        InsertData<clientheader>(client);

        listData<clientheader>(client);
        Console.ReadKey();
    }

    static void showHeader<t>(string[,] matrix)
    {
        string line = new String('-', 83);
        int[] size = new int[] { 4, 10, 10, 15, 10, 10, 15 };
        Console.WriteLine(line);
        Console.Write("|");
        for (int i = 0; i < matrix.GetLength(1) - 1; i++)
        {
            string space = new String(' ', size[i] - GetHeader<t>(i).Length);
            string header = GetHeader<t>(i);
            Console.Write($"{header.ToUpper()}{space}");
            Console.Write("|");
        }
        Console.WriteLine();
    }

    static void listData<t>(string[,] matrix)
    {
        Console.Clear();
        showHeader<t>(matrix);

        string line = new String('-', 83);
        int[] size = new int[] { 10, 15, 15, 15, 15, 15, 15 };

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            Console.WriteLine(line);
            Console.Write("|");
            for (int j = 0; j < matrix.GetLength(1) - 1; j++)
            {
                if (matrix[i, j] == null) matrix[i, j] = "";
                string space = new String(' ', size[i] - matrix[i, j].Length - 1);
                Console.Write($"{matrix[i, j]}{space}");
                Console.Write("|");
            }
            Console.WriteLine();
        }
        Console.WriteLine(line);
    }

    static int getInsertIndex(string[,] matrix)
    {
        for (int j = 0; j < matrix.GetLength(0); j++)
        {
            if (string.IsNullOrEmpty(matrix[j, 0])) return j;
        }

        return -1;
    }

    private static void InsertData<t>(string[,] matrix)
    {
        int n = getInsertIndex(matrix), id = 1;

        matrix[n, 0] = Convert.ToString(id++);
        int x = matrix.GetLength(1) - 1;

        for (var j = 1; j < matrix.GetLength(1); j++)
        {
            do
            {
                Console.Write($"\nInsert {GetHeader<t>(j)}: ");
                matrix[n, j] = Console.ReadLine();
            } while (string.IsNullOrEmpty(matrix[0, j]));
        }
    }

    private static string GetHeader<t>(int i) => Enum.GetName(typeof(T), i);

    enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };


    }
}
Posted
Updated 19-Aug-18 23:55pm
v2
Comments
Nelek 19-Aug-18 15:52pm    
What do you mean with "bad formatation"? Is it a bad format or a bad formation?
What do you expect? What are you getting?

This is just a guess as you have not explained the problem. But, you probably need to use field width, and possibly alignment, settings to get all your values to line up in columns. See $ - string interpolation (C# Reference) | Microsoft Docs[^].
 
Share this answer
 
Comments
Leonardo Guimarães 20-Aug-18 4:57am    
I'm sorry if I have'nt been clear but the problem is I need to output the matrix such as a table, and I don't get that as output even tough I don't know why
Richard MacCutchan 20-Aug-18 5:04am    
Thank you, but ... that tells us nothing. How are we to guess what format you want, and what format you actually see?

Try writing out the details on paper, then decide how wide each column needs to be, whether the content is left or right aligned, etc. Once you have done that you can figure out the details for your format strings.
-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------


I want something like that, also with the matrix data within....
 
Share this answer
 
Comments
Richard MacCutchan 20-Aug-18 6:26am    
OK, so now you have your template you just need to count the widths of each field and use those values in your format strings.
Leonardo Guimarães 20-Aug-18 6:47am    
the problem is that even doing such as you said, counting the width I haven't got a table as shown above
Richard MacCutchan 20-Aug-18 7:09am    
Then you are doing something wrong, but we cannot guess what that is. Formatting text for output to the console is very easy because consoles use fixed width characters. Whatever you are doing your field width settings must be wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900