English 中文(简体)
C# 在清单中添加含有另一类成分的物品和清单<Class”。
原标题:C# Adding Items to a List that contain both another Class along with a List<Class>
  • 时间:2023-12-31 23:02:26
  •  标签:
  • c#
  • list
  • class

我在多天时间里一直在搜索因特网,试图找到我找不到的答案。 我已尝试过对法典的多种改动,结果完全相同。

我建造了两间小班,即“Names”和“Names”学生。 然后,我试图将这两者纳入另一类称为类别名单。 一切都是有益的,直到我把最后一个项目列入议程。 我可以把事情打破,但我会知道我如何能够在“单一”指挥下这样做。 我的印象是,必须这样做,但我对我应该做些什么感到 el。 以前的一切工作:普通静读清单MeadowlarkClasses = 新的()

namespace Testing
{
    internal class Testing
    {
        public class clsStudentName
        {
            public int? StudentID { get; set; }
            public string? StudentFirstName { get; set; }
            public string? StudentMiddleInitial { get; set; }
            public string? StudentLastName { get; set; }


            public clsStudentName()
            {

            }

            public clsStudentName(
                int? intStudentID,
                string? strStudentFirstName,
                string? strStudentMiddleInitial,
                string? strStudentLastName)
            {
                StudentID = intStudentID;
                StudentFirstName = strStudentFirstName;
                StudentMiddleInitial = strStudentMiddleInitial;
                StudentLastName = strStudentLastName;

            }

        }

        public class clsClassName
        {
            public int? ClassID { get; set; }
            public string? ClassName { get; set; }

            public clsClassName()
            {

            }

            public clsClassName(
                int intClassID,
                string strClassName
                )
            {
                ClassID = intClassID;
                ClassName = strClassName;

            }

        }

        public class clsClasses
        {
            public clsClassName? clsClassNm { get; set; } = new clsClassName();
            public List<clsStudentName>? clsStudentNames { get; set; } = new List<clsStudentName>();

            clsClasses()
            {

            }

            public clsClasses(
                clsClassName? ClassNm,
                List<clsStudentName>? lstStudentNames
                )
            {
                clsClassNm = ClassNm;
                clsStudentNames = lstStudentNames;
            }
        }

        public static readonly clsClassName MathClass = new(
            1,
            "Math"
            );

        public static readonly clsClassName ArtClass = new(
            2,
            "Art"
            );

        public static readonly clsStudentName AndyApple = new(
            1,
            "Andy",
            "",
            "Apple"
            );

        public static readonly clsStudentName BettyBoo = new(
            2,
            "Betty",
            "",
            "Boo"
            );

        public static readonly clsStudentName CarlCrown = new(
            3,
            "Carl",
            "",
            "Crown"
            );

        public static readonly clsClasses MathClasses = new(
            MathClass,
            new List<clsStudentName>()
            {
                AndyApple,
                BettyBoo
            }
            );

        public static readonly clsClasses ArtClasses = new(
            ArtClass,
            new List<clsStudentName>()
            {
                BettyBoo,
                CarlCrown
            }
            );

        private static readonly List<clsClasses> MeadowlarkClasses = new()
            {
                new() 
                {
                    new clsClassName(1, "Math"),
                    new List<clsStudentName>()
                        {
                            new()
                            {
                                StudentID = 1,
                                StudentFirstName = "Mother",
                                StudentLastName = "Teresa"
                            },
                            new()
                            {
                                StudentID = 2,
                                StudentFirstName = "Father",
                                StudentLastName = "Dowling"
                            }

                        }

                }

            };

    }

}

我破碎了事情,试图获得更多的信息,试图在较小的步骤中加以利用,以图示我错做的事情,但我似乎无法找到答案。 我感谢任何人所能提供的任何帮助。

问题回答

Your code, as posted in the question, is more or less fine. You really just needed to get the syntax right in your MeadowlarkClasses declaration. It was not a fundamental issue with how you defined your classes or C#. You just got the syntax wrong.

Also, as it has been pointed out, your naming convention is horrible. No doubt that made it harder to reason with your code. And, you had mixed constructors that weren t needed.

我向你们提出的建议是,重新调整你的班级,并落实这些班子。

public class Student(int? id, string? firstName, string? middleInitial, string? lastName)
{
    public int? Id => id;
    public string? FirstName => firstName;
    public string? MiddleInitial => middleInitial;
    public string? LastName => lastName;
}

public class Subject(int id, string name)
{
    public int? Id => id;
    public string? Name => name;
}

public class Enrolments(Subject? subject, List<Student>? students)
{
    public Subject? Subject => subject;
    public List<Student>? Students => students;
}

Now the rest of your code is straightforward:

public static readonly Subject Math = new(1, "Math");
public static readonly Subject Art = new(2, "Art");
public static readonly Student AndyApple = new(1, "Andy", "", "Apple");
public static readonly Student BettyBoo = new(2, "Betty", "", "Boo");
public static readonly Student CarlCrown = new(3, "Carl", "", "Crown");

public static readonly Enrolments MathEnrolments = new(Math, new List<Student>() { AndyApple, BettyBoo } );
public static readonly Enrolments ArtEnrolments = new(Art, new List<Student>() { BettyBoo, CarlCrown } );
private static readonly List<Enrolments> MeadowlarkEnrolments = new()
{
    new Enrolments(
        Math,
        new List<Student>()
        {
            new Student(1, "Mother", "", "Teresa"),
            new Student(2, "Father", "", "Dowling"),
        })
};

法典存在几个问题:

  1. <代码>clsClasses 需有<条码> 公开,以便在初始化过程中使用。

  2. You ve mixed up the constructor and initialization syntax a bit.

这本书汇编——我不清楚你是否打算使用建筑商或开工,因此我与后者联系。


/*

Assumes the constructor is public:

public clsClasses()
{
}

*/
private static readonly List <clsClasses> MeadowlarkClasses = new() {
    new clsClasses {
        clsClassNm = new clsClassName(1, "Math"),
        clsStudentNames = new List <clsStudentName> () {
            new() {
                StudentID = 1,
                StudentFirstName = "Mother",
                StudentLastName = "Teresa"
            },
            new() {
                StudentID = 2,
                StudentFirstName = "Father",
                StudentLastName = "Dowling"
            }
        }
    }
};

重复评论中所说的话——指明事情是困难的,命名公约可以被忽视,但我在此表示意见。

One thing I would highly recommend is dropping the Hungarian notation (the cls) as it s very easy to see in the IDE that this does represent a class, and extra noise in the name - even noise which seems like it could be helpful - makes it harder to tell what something represents at a glance.

尽管我真心实意是,在C#中,“<代码>><>>>/代码”的概念的确代表了一种类别——从教训的意义上来说,如此的话,我们大脑中却很难形成另一个名称。 我将选择一个独一无二的班子,因为班级代表一个主题和一个学生名单。 <代码>Course, 或许? 我认为,这取决于这种教育的哪一种。

And - for the sake of completeness - the clsStudentName class contains the student name and the ID, so something like Student would be a better name.

我希望你不要忘记这一令人不快的建议——过去,我曾多次错过坏的阶级名称造成的错误,因此,现在我确实尽量清楚这些错误。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签