Polymorphism; Update functions and accessibility

This is a discussion on Polymorphism; Update functions and accessibility within the C# Programming forums, part of the General Programming Boards category; Hi all. This is the first time I've registered to a programming forum since usually I rely on searching the ...

Polymorphism; Update functions and accessibility

Hi all. This is the first time I've registered to a programming forum since usually I rely on searching the internet, but the following problem seems a bit complicated. Appologies if my noobishness makes you hurl

Anyways, I'm currently writing a game in XNA. As part of it I've got a base class for all of the movable objects in my game and have derived a player class which is used to create all of the object classes for players. As part of that I've added a Update class with the intention of calling the other Update class in the base class. This currently looks like this;

Now there's a few problems with this. In my main program I've created an array of ActorBase objects with the intention of 1: Substantiating all of my player objects in it and 2: Adding further NPC objects which will also derive from ActorBase. Insofar as my knowledge of programming goes this is perfectly valid, and the compiler doesn't throw me an error when I do this so I assume I'm on the right track. At any rate, my program doesn't want to call the new Update function of the Player class. This is what I've got (a few other bits of code are also present but I won't put them here for the sake of clarity);

Compiler error happens with the Update function, claiming that it can't access it. This makes sense if it's calling the ActorBase Update function but given that I initialized it as a Player class, shouldn't it call that Update function instead? Also for future reference, when I'm using foreach loops to call individual objects in the array, is it valid to refer to say foreach (Player player in obj)?

According to what you posted your ActorBase Update() method is protected, meaning that only classes that derive from it will be able to call this function. Instead you should make this public. Polymorphism won't happen until runtime, therefore to the compiler it just see's you trying to call a protected method and lets you know politely by not compiling.

Yes you can use a foreach in this manner becasue afterall ActorBase "Is-A" Player.

I like the idea of the compiler being polite. I guess an impolite compiler would say "Hey idiot, you can't call protected members. What are you, stupid?" :P Making it public makes sense since I never really use the BaseClass in the construction of any objects in-game, so it wouldn't matter too much if I were to make it public. Now though it seems that when I create the object in the same way as I did before, the program goes directly to the ActorBase's Update function, rather than going to the Player Update function. Do I have to specify certain function attributes in order for the compiler to use the Player Update rather than the ActorBase one?

EDIT: With a bit of testing, I've confirmed that the compiler, though I've specifically called the class and constructor for a Player object, is still considering obj[0] as an ActorBase class. When I add an empty function to Player the program doesn't recognise it as a valid function as part of obj[0], even though obj[0] in theory should be a Player object.