locked
Classes RRS feed

  • Question

  • User-797751191 posted

    Hi

      I have below class and i want how  StudentAddress & Grade works

    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public float Weight { get; set; }
            
        public StudentAddress StudentAddress { get; set; }
        public Grade Grade { get; set; }
    }

    Thanks

    Thursday, October 17, 2019 6:55 AM

All replies

  • User-719153870 posted

    Hi jsshivalik,

    how  StudentAddress & Grade works

    I think it's easy to understand, this StudentAddress is just like a string or int and you instantiate them as StudentAddress and StudentName or StudentID. It's the same thing.

    But i won't recommand you set the instanced object name as same as your class.

    It should be like, this can lead to unexpected errors:

    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public float Weight { get; set; }
            
        public StudentAddress SAddress { get; set; }
        public Grade SGrade { get; set; }
    }

    You can also refer to Classes (C# Programming Guide) and Classes and Structs (C# Programming Guide) for more information.

    Best Regard,

    Yang Shen

    Thursday, October 17, 2019 7:40 AM
  • User1120430333 posted

    public StudentAddress StudentAddress { get; set; }
    public Grade Grade { get; set; }

    Those are objects within the Student object.

    To access the properties of the objects in student you would have to address like this.

    var student = new Student();

    student.StudentAddress  = new StudentAddress();

    student.Grade =new Grade();

    student.StudentName = "jasshvalik";

    student.StudentAddress.Address = "no where";

    student.Grade.TheGrade = "F";

    OO is OO .NET of Java.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Thursday, October 17, 2019 12:10 PM