数组2D GetUpperBound(1)返回4而不是8

时间:2018-08-30 15:26:39

标签: c# multidimensional-array

我是C#的新手,我正在尝试输出2D几个数组的历史记录,但是我不明白为什么函数GetUpperBound无法正常工作。我进行了调试,在switch语句中以可视化的方式显示了Appoitments,它在最后一列返回4。

如果称为Appointments的矩阵有9列,为什么matrix.getupperbound(1)返回4?很抱歉,如果其中包含大量代码,请提供任何帮助。

     enum clientHeader { Id_Client, Name, Type, Description, Name_CLient, Surname, Id_Spelciaty, Final_add,State };

            enum appointHeader { Id_Appoint, Name_Client, Surname,Type_Appoint,Date, Description, Id_Client, Final_add,State};

static void Main(string[] args)
            {
                Console.SetWindowSize(146, 45);

                string[,] animal = new string[30, 9];
                string[,] client = new string[30, 9];
                string[,] vet = new string[30, 9];
                string[,] appointment = new string[30, 9];
                string[,] obsRooms = new string[30, 5];
                string[,] typeAnimal = new string[30, 5];
                string[,] spelciaty = new string[30, 5];

                do { menuHistoric(animal, client, vet, obsRooms, appointment, spelciaty, typeAnimal); } while (true);
            }
            static void showHeader<T>(string[,] matrix)
            {
                int x = matrix.GetUpperBound(1), width = (tableWidth / x);
                Console.Clear(); 
                Console.WriteLine("\n");
                PrintLine();

                for (int i = 0; i < matrix.GetLength(1)-1; i++)
                {
                    Console.Write((GetHeader<T>(i)?.Length > width) ? $"{GetHeader<T>(i)?.Substring(0, width - 3) + ".." + "|".ToUpper()}" : $"{ GetHeader<T>(i)?.PadRight(width - (width - GetHeader<T>(i).Length) / 2).PadLeft(width).ToUpper()}|");
                }
                Console.WriteLine();
                PrintLine();
            }
            static int tableWidth = 143;

            static void PrintLine()
            {
                Console.WriteLine(new string('-', tableWidth));
            }
               static void menuHistoric(string[,] animal, string[,] client, string[,] vet, string[,] appointment, string[,] obsRooms, string[,] spelciaty, string[,] typeAnimal)
            {

                while (true)
                {
                    int optHist;

                    do
                    {
                        Console.Clear();
                        Console.WriteLine("\nInsert:\n\t[ 1 ] Visualize historic Clients");
                        Console.Write("\t[ 2 ] Visualize historic Appointments ");


                    } while (!int.TryParse(Console.ReadLine(), out optHist) || optHist < 0 || optHist > 7);

                    Console.Clear();
                    bool goBack = false;

                    switch (optHist)
                    {
                        case 1:
                           showHeader<clientHeader>(client);
                            menuReturn();
                            break;
                        case 2:
                           showHeader<appointHeader>(appointment);
                            menuReturn();
                            break;
                        case 0:
                            goBack = true;
                            break;

                    }
                    if (goBack) return;
                }
            }
            static void menuReturn()
            {
                Console.ReadKey();
                Console.Clear();
            }
            private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

1 个答案:

答案 0 :(得分:0)

您混淆了以下参数:
menuHistoric(动物客户端兽医 obsRooms ,约会,安全性,typeAnimal)
static void menuHistoric(string [,] animal ,string [,] client ,string [,] vet ,string [,] 任命,string [,] obsRooms,string [,] spelciaty,string [,] typeAnimal)

也许可以通过使用MenuHistoricArgument的类/结构来避免使用大量参数:

    enum clientHeader { Id_Client, Name, Type, Description, Name_CLient, Surname, Id_Spelciaty, Final_add, State };

    enum appointHeader { Id_Appoint, Name_Client, Surname, Type_Appoint, Date, Description, Id_Client, Final_add, State };

    struct MenuHistoricArgument
    {
        public string[,] animal, client, vet, appointment, obsRooms, typeAnimal, spelciaty;
    }

    static void Main (string[] args)
    {
        Console.SetWindowSize(146, 45);

        MenuHistoricArgument menuArgs = new MenuHistoricArgument()
        {
            animal = new string[30, 9],
            client = new string[30, 9],
            vet = new string[30, 9],
            appointment = new string[30, 9],
            obsRooms = new string[30, 5],
            typeAnimal = new string[30, 5],
            spelciaty = new string[30, 5],
        };

        do {
            menuHistoric(menuArgs);
        } while (true);
    }
    static void showHeader<T> (string[,] matrix)
    {
        int x = matrix.GetUpperBound(1), width = (tableWidth / x);
        Console.Clear();
        Console.WriteLine("\n");
        PrintLine();

        for (int i = 0; i < matrix.GetLength(1) - 1; i++)
        {
            Console.Write((GetHeader<T>(i)?.Length > width) ? $"{GetHeader<T>(i)?.Substring(0, width - 3) + ".." + "|".ToUpper()}" : $"{ GetHeader<T>(i)?.PadRight(width - (width - GetHeader<T>(i).Length) / 2).PadLeft(width).ToUpper()}|");
        }
        Console.WriteLine();
        PrintLine();
    }
    static int tableWidth = 143;

    static void PrintLine ()
    {
        Console.WriteLine(new string('-', tableWidth));
    }
    static void menuHistoric (MenuHistoricArgument menuArgs)
    {

        while (true)
        {
            int optHist;

            do
            {
                Console.Clear();
                Console.WriteLine("\nInsert:\n\t[ 1 ] Visualize historic Clients");
                Console.Write("\t[ 2 ] Visualize historic Appointments ");


            } while (!int.TryParse(Console.ReadLine(), out optHist) || optHist < 0 || optHist > 7);

            Console.Clear();
            bool goBack = false;

            switch (optHist)
            {
                case 1:
                    showHeader<clientHeader>(menuArgs.client);
                    menuReturn();
                    break;
                case 2:
                    showHeader<appointHeader>(menuArgs.appointment);
                    menuReturn();
                    break;
                case 0:
                    goBack = true;
                    break;

            }
            if (goBack) return;
        }
    }
    static void menuReturn ()
    {
        Console.ReadKey();
        Console.Clear();
    }
    private static string GetHeader<T> (int i) => Enum.GetName(typeof(T), i);

(向您发送大量代码!)