开发者

trying to make a Class

I am trying to make this work myClass.student[x].books[z].开发者_StackOverflow

For each student I need to collect a name and a array of strings(books).

public class myClass
{
    public student[] batch;
}

public class student
{
    public books[] ;
    public string naam{ get; set; }
}


 public class MyClass
    {
        public List<student> Students;
    }

    public class student
    {
        public List<Book> Books;
        public string name{ get; set; }
    }

    public class Book
    {
        public string name{ get; set; }
        public string Author;
        Public string publisher;
    }


Similar to @MBen's answer, but with naming/casing conventions cleaned up and default collection initialization.

Also, unless there is a good reason to expose them publicly, I keep the setters on my collections private (but that's personal preference).

Arrays will be very inflexible for this type of object model; a resizable collection is far easier to work with in most cases, but the call syntax will be the same to work with as an array.

public class SchoolClass // avoid collision with the "class" keyword
{
    public SchoolClass()
    {
        this.Students = new List<Student>();
    }

    public List<Student> Students
    {
       get; 
       private set;
    }
}

public class Student
{
    public Student()
    {
        this.Books= new List<Book>();
    }

    public List<Book> Books
    {
       get; 
       private set;
    }

    public string Name
    {
        get;
        set;
    }
}

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

    public string Author
    {
        get;
        set;
    }

    public string Publisher
    {
        get;
        set;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜