如何为嵌套类创建索引器?

问题描述 投票:0回答:0

大学作业。需要创建将存储类 B 的类 A,类 B 将存储类 C。“存储”可能只是 ARRAY,没有别的。对于前。 A 类是一个“Section”,它将存储类“Group”(Group[]),它将存储类“GroupMember”(GroupMember[])。 在这些类之间传递数据的唯一允许方式是使用索引器和属性。此外,所有这些都必须以菜单形式打包,以便用户可以添加、读取每个类的数据。 这是我的看法。我经常遇到越界索引器或 null inst 的错误。错误。 下面是 NestedClasses .cs 文件

    public class Section
    {
        public string Name { get; set; }

        Group[] own;
        public int Length;
        public Section(int Len, Group[] PIDARAS)
        {
            Length = Len;
            own = PIDARAS = new Group[Len];


        }
        public Group this[int index]
        {
            get
            {
                return own[index];
            }
            set
            {
                own[index] = value;
            }
        }
       
    }
    public class Group
    {
        public string Name { get; set; }
        GroupMember[] personalInfo = new GroupMember[5];
        public Group(params GroupMember[] personalInfo)
        {
            personalInfo.Initialize();
        }
        public GroupMember this[int index]
        {
            get => personalInfo[index];
            set => personalInfo[index] = value;
        }
    }
    public class GroupMember
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public GroupMember(string firstname, string lastName)
        {
            FirstName = firstname;
            LastName = lastName;
        }

    }
} ```

And here is the menu part

``` public class Program
    {

        private static bool hasRun = false;
        private static int Gr = 0;
        private static int Ptc = 0;
        private static Section sectionDB = new Section(5, new Group[]{});

        static void Main()
        {
            sectionDB.Name = "IHateIndexers";

            if (hasRun == false)
            {
                Console.WriteLine("Hello b0ss, how many section do u want to have?");
            }

            if (hasRun == false)
            {
                //const int sectionsAmount = 5;
            }

            Console.Clear();
            ConsoleKey KeyPress;
            while (true)
            {
                Console.WriteLine("Choose action:" +
                                                    "\n-1 Add ... " +
                                                    "\n-2 Show Gr Members. " +
                                                    "\n-3 Show Groups" +
                                                    "\n-4 Show Section" +
                                                    "\n-5 Search..." +
                                                    "\n-<ESC> Escape.");
                KeyPress = Console.ReadKey(true).Key;
                switch (KeyPress)
                {
                    case (ConsoleKey.D1):
                        Console.Clear();
                        CustomAdd();
                        break;
                    case (ConsoleKey.D2):
                        Console.Clear();
                        ShowParticipants();
                        break;
                    case (ConsoleKey.D3):
                        Console.Clear();
                        ShowGroups();
                        break;
                    case (ConsoleKey.D4):
                        Console.Clear();
                        ShowSection();
                        break;
                    case (ConsoleKey.D5):
                        Console.Clear();
                        //CustomSearch();
                        break;
                    case (ConsoleKey.Escape):
                        Console.Clear();
                        Environment.Exit(0);
                        break;
                    default:
                        Console.Clear();
                        Console.WriteLine("No Such operation.");
                        break;
                }
            }
        }



        static void CustomAdd()
        {
            ConsoleKey KeyPress;
            while (true)
            {
                Console.WriteLine("Choose action:" +
                                                    "\n-1 Add Group member " +
                                                    "\n-2 Add Group " +
                                                    "\n-3 Add Section" +
                                                    "\n-<ESC> Back.");
                KeyPress = Console.ReadKey(true).Key;
                switch (KeyPress)
                {
                    case (ConsoleKey.D1):
                        Console.Clear();
                        AddParticipant();
                        break;
                    case (ConsoleKey.D2):
                        Console.Clear();
                        AddGroup();
                        break;
                    case (ConsoleKey.D3):
                        AddSection();
                        break;
                    case (ConsoleKey.D4):
                        Console.Clear();
                        break;
                    case (ConsoleKey.Escape):
                        Console.Clear();
                        Main();
                        break;
                    default:
                        Console.Clear();
                        Console.WriteLine("no such operation.");
                        break;
                }
            }
        }

        static void AddParticipant()
        {
            Console.Clear();
            //   sectionDB[Gr][Ptc].Name = Console.ReadLine();
            Ptc++;
            Console.WriteLine("Group Added Succesfully");
        }

        static void AddSection()
        {
            Console.Clear();
            sectionDB.Name = Console.ReadLine();
            Console.WriteLine("Section Added Succesfully");
        }
        static void AddGroup()
        {
            Console.Clear();
            Console.WriteLine(sectionDB.Name);
            sectionDB[Gr].Name = Console.ReadLine();
            Gr++;
            Console.WriteLine("Group Added Succesfully");
        }
        static void ShowSection()
        {
            Console.WriteLine(sectionDB.Name);
        }

        static void ShowGroups()
        {
            Console.WriteLine(Gr);
            for (int i = 0; i < Gr; i++)
            {
                Console.WriteLine(sectionDB[i].Name);
            }
        }
        static void ShowParticipants()
        {
            Console.WriteLine(Gr);
            for (int i = 0; i < Gr; i++)
            {
                for (int j = 0; j < Ptc; j++)
                {
                    //     Console.WriteLine(sectionDB[i][j].Name);
                }
            }
        }
    }
} ```
c# indexer
© www.soinside.com 2019 - 2024. All rights reserved.