The Artima Developer Community
Sponsored Link

.NET Buzz Forum
A simple question about overloading

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
A simple question about overloading Posted: Jan 6, 2005 4:41 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: A simple question about overloading
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

Given 3 classes: PersonBase, Employee and Manager:

class PersonBase { }

class Employee : PersonBase { }

class Manager : Employee { }

 

and given the following  methods:

 

public void Loop()

 {

     PersonBase[] people = new PersonBase[]{

                                 new PersonBase(),

                                 new Employee(),

                                 new Manager()

                                 };

 

     foreach (PersonBase pb in people)

     {

         doSomething(pb);

     }

 }

 

 

 void doSomething(PersonBase p)

 {

     Console.WriteLine("PersonBase");

 }

 void doSomething(Employee e)

 {

     Console.WriteLine("Employee");

 }

 void doSomething(Manager m)

 {

     Console.WriteLine("Manager");

 }

 

Why does only the first of the three “doSomething” overloads get called for all the objects in the array?

How could you force those methods to get called?

(Ignore the various class factory solutions out there.)

Read: A simple question about overloading

Topic: Freeware tools and components Previous Topic   Next Topic Topic: Helping w/ the Tsunami Relief Effort

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use