Question about passing inherited objects in methods

Hi experts,

I have a class called student that inherits from a person class.  I wanted to update the student's name only so I created a method in my person repository called Update(Person p) that accepts a person.

public Person
{
public string Name;
}

public Student : Person
{
public string StudentID
}
....
Person Repository
{
public void Update(Person p)
}

xxx some where in my program xxx

Student student = Repository.GetStudent();
student.Name = "Jack";
Repository.Update(student);

I know that the Update method will contain a person object that I can interact with but HOW is the data being sent to the repository? Does the student object get sent to the repository or does .Net automatically extract the Person object from the Student object then sends it to the repository?
matthew phungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>I have a class called student that inherits from a person class.
That means that a 'student' is a 'person' plus some extra stuff.  So anything that accepts an object that is a 'person' also means it will accept any object based on a 'person' such as a 'student'.  This update method you call works with a 'person' part of the student object.
Does that make it any clearer to you?

Note the reverse does NOT work.  You can not pass a 'person' into something that expects a 'student'.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
If I told you to, "Go get into that Lamborghini over there," what would you do? You'd open the door, then get in, right? If I then told you, "Go get into that Ford F150 over there," what would you do? You'd do the exact same thing, right?

Even though each automobile has a door that you can open, each door functions differently? I passed two different automobiles to you, and you still knew how to get into them. I didn't chunk out automobile functionality...I gave you the whole car and whole truck in each case. The same thing happens in your code. .NET doesn't do anything special with the object itself. Rather the whole object is passed, and the contract set forth by the base class gives .NET enough information to know that each person has a specific set of methods and propeties.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.