none
Need explanation - Passing object as a method parameter RRS feed

  • Question

  • Hi all,

    I am kind of confused here, not sure if the right place to clarify, but here goes. The code is part of the Microsoft Lab exercise from the 20483# course

    I have got this class : - 

    public class Student: IComparable<Student>
        {
            public int StudentID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
            // Constructor to initialize the properties of a new Student
            public Student(int studentID, string firstName, string lastName)
            {
                StudentID = studentID;
                FirstName = firstName;
                LastName = lastName;
               
            }
    
            // Default constructor 
            public Student()
            {
                StudentID = 0;
                FirstName = String.Empty;
                LastName = String.Empty;
                
            }
    
            
    
            // Compare Student objects based on their LastName and FirstName properties
            // so we are actually passing the student object which is named as other
            // objects are also instances
            public int CompareTo(Student other)
            {
                // Concatenate the LastName and FirstName of this student
                string thisStudentsFullName = LastName + FirstName;
    
                // Concatenate the LastName and FirstName of the "other" student
                string otherStudentsFullName = other.LastName + other.FirstName;
    
                // Use String.Compare to compare the concatenated names and return the result
                return(String.Compare(thisStudentsFullName, otherStudentsFullName));
            }

    Notice the CompareTo method, I know I am implementing the interface here, but my question is on the "Student other", the object of Student which is passed as a parameter in the method.  

    Object is an instance of a class right and is created by using the "NEW" keyword. How is that then the method takes the object as a parameter when no new instance has been created? I am lost here. I am newbie.


    Thursday, March 15, 2018 2:40 PM

All replies

  • Object is an instance of a class right and is created by using the "NEW" keyword. How is that then the method takes the object as a parameter when no new instance has been created? I am lost here. I am newbie.

    Class vs object vs instance, and OO is OO .NET or Java. 

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

    The object  could have been instanced elsewhere in code and passed as a parm to the method you are talking about. 

    Thursday, March 15, 2018 3:37 PM
  •  the method CompareTo  is a method of the Class Student.

    It is used inside an instanced Student object where it takes another instanced Student object as a parameter. 

    Student A = new Student();
    Student B = new Student();
    int C = A.CompareTo(B); 


    Success
    Cor


    • Edited by Cor Ligthert Thursday, March 15, 2018 3:54 PM
    • Proposed as answer by Tim Roberts Friday, March 16, 2018 6:31 AM
    Thursday, March 15, 2018 3:53 PM