c# 4.0 - C# Calling an overriden method within the superclass -


hey there. picked c# learn game programming xna, have experience in java.

here's code, in essence:

public class {     public rectangle getrectangle()     {         return [some rectangle];     }      public bool collision(a other)     {         rectangle rect1 = getrectangle();         rectangle rect2 = other.getrectangle();         return rect1.intersects(rect2);     } }  public class b : {     public rectangle getrectangle()     {         return [some other rectangle];     } } 

the problem arises when try this:

a a; b b; if(a.collision(b))     ... 

where b's version of rectangle never called, far can tell. tried solution 1 suggested here message "b.getrectangle() hides inherited member a.getrectangle(). use new keyword if intended."

i appreciate in advance receive. i'm thinking past java experience getting in way of understanding how c# different. guess if knows of link explains differences between c# , java or how c# works in respect suffice.

cheers.

unlike java, methods in c# not marked virtual default. current code doing hiding getrectangle method: there implicit new modifier on declaration of method in derived class.

you need explicitly include virtual modifier in method-declaration in base class:

public virtual rectangle getrectangle() { ... } 

and override in derived class with:

public override rectangle getrectangle() { ... } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -