Jump to content
  • Advertisement
Sign in to follow this  
ravinDavin

Polymorphism not working as expected - C#

This topic is 2560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?

Share this post


Link to post
Share on other sites
Advertisement

Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)

Share this post


Link to post
Share on other sites

[quote name='ravinDavin' timestamp='1323523123' post='4892502']
Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)
[/quote]

The reason I check the type is because Obstacle is a child of a CollidableSprite, which inturn is a child of Sprite.
So, I am looping through my list of sprites on screen, and finding the obstacles. If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.

And I do understand polymorphism, if I have many different obstacle objects all inheriting from the base obstacle class which as the onCollide method, I should be able to cast the sprite as an obstacle and call the onCollide, depending on what type of obstacle it was, the correct onCollide will be called, which is what I am trying to accomplish.

Share this post


Link to post
Share on other sites

[quote name='Idov' timestamp='1323523935' post='4892508']
[quote name='ravinDavin' timestamp='1323523123' post='4892502']
Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)
[/quote]

The reason I check the type is because Obstacle is a child of a CollidableSprite, which inturn is a child of Sprite.
So, I am looping through my list of sprites on screen, and finding the obstacles. If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.

And I do understand polymorphism, if I have many different obstacle objects all inheriting from the base obstacle class which as the onCollide method, I should be able to cast the sprite as an obstacle and call the onCollide, depending on what type of obstacle it was, the correct onCollide will be called, which is what I am trying to accomplish.
[/quote]

Ploymorphism = No casting required.
Polymorphism enables you to have an array of "Sprite"s, and run different implementations of the same interface without knowing exactly what types of "Sprite"s you're having.
The fact that you try to *cast* your sprite to an "obstacle" implies that you're doing something basically wrong.



and again, without seeing your code it will be pretty hard to know what is not working in it.

Share this post


Link to post
Share on other sites

If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.
There's your problem, typeof is literally looking at the type, a bouncepad has typeof BouncePad not Obstacle. You can use the [font="'Courier New"]is[/font] operator which will pay attention to the inheritance chain as you need it to, although more than likely it's the [font="'Courier New"]as[/font] operator you want which will also do the cast for you.

Share this post


Link to post
Share on other sites

[quote name='ravinDavin' timestamp='1323525731' post='4892512']
[quote name='Idov' timestamp='1323523935' post='4892508']
[quote name='ravinDavin' timestamp='1323523123' post='4892502']
Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)
[/quote]

The reason I check the type is because Obstacle is a child of a CollidableSprite, which inturn is a child of Sprite.
So, I am looping through my list of sprites on screen, and finding the obstacles. If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.

And I do understand polymorphism, if I have many different obstacle objects all inheriting from the base obstacle class which as the onCollide method, I should be able to cast the sprite as an obstacle and call the onCollide, depending on what type of obstacle it was, the correct onCollide will be called, which is what I am trying to accomplish.
[/quote]

Ploymorphism = No casting required.
Polymorphism enables you to have an array of "Sprite"s, and run different implementations of the same interface without knowing exactly what types of "Sprite"s you're having.
The fact that you try to *cast* your sprite to an "obstacle" implies that you're doing something basically wrong.



and again, without seeing your code it will be pretty hard to know what is not working in it.
[/quote]

Mate, Of course I will need to cast it to a certain type. Child classes will more than likely have different methods than their parent, if you have a method that you want to call in a child class, you won't be able to call that method from its parent, it won't know what it is. The virtual method I was trying to invoke was in the Obstacle class, Meaning only an obstacle object could access it, meaning I needed a cast from my parent Sprite class. Thanks for your help anyway.

Share this post


Link to post
Share on other sites

[quote name='ravinDavin' timestamp='1323525731' post='4892512']
If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.
There's your problem, typeof is literally looking at the type, a bouncepad has typeof BouncePad not Obstacle. You can use the [font="Courier New"]is[/font] operator which will pay attention to the inheritance chain as you need it to, although more than likely it's the [font="Courier New"]as[/font] operator you want which will also do the cast for you.
[/quote]

Awesome thanks!

Share this post


Link to post
Share on other sites

[quote name='Idov' timestamp='1323529489' post='4892527']
[quote name='ravinDavin' timestamp='1323525731' post='4892512']
[quote name='Idov' timestamp='1323523935' post='4892508']
[quote name='ravinDavin' timestamp='1323523123' post='4892502']
Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)
[/quote]

The reason I check the type is because Obstacle is a child of a CollidableSprite, which inturn is a child of Sprite.
So, I am looping through my list of sprites on screen, and finding the obstacles. If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.

And I do understand polymorphism, if I have many different obstacle objects all inheriting from the base obstacle class which as the onCollide method, I should be able to cast the sprite as an obstacle and call the onCollide, depending on what type of obstacle it was, the correct onCollide will be called, which is what I am trying to accomplish.
[/quote]

Ploymorphism = No casting required.
Polymorphism enables you to have an array of "Sprite"s, and run different implementations of the same interface without knowing exactly what types of "Sprite"s you're having.
The fact that you try to *cast* your sprite to an "obstacle" implies that you're doing something basically wrong.



and again, without seeing your code it will be pretty hard to know what is not working in it.
[/quote]

Mate, Of course I will need to cast it to a certain type. Child classes will more than likely have different methods than their parent, if you have a method that you want to call in a child class, you won't be able to call that method from its parent, it won't know what it is. The virtual method I was trying to invoke was in the Obstacle class, Meaning only an obstacle object could access it, meaning I needed a cast from my parent Sprite class. Thanks for your help anyway.
[/quote]

I think what he is saying is that you are misusing polymorphism quite heavily, if child classes have different methods than their parents then you should most likely store and deal with them separatly and reconsider if they really should inherit from that parent at all. (In your case it would make far more sense to store your Obstacles in their own list and give those a Sprite member rather than having them inherit from Sprite, Let your renderer keep a list of sprites and keep all graphical stuff away from the game logic).

Share this post


Link to post
Share on other sites

[quote name='ravinDavin' timestamp='1323534049' post='4892543']
[quote name='Idov' timestamp='1323529489' post='4892527']
[quote name='ravinDavin' timestamp='1323525731' post='4892512']
[quote name='Idov' timestamp='1323523935' post='4892508']
[quote name='ravinDavin' timestamp='1323523123' post='4892502']
Hi, I have a base class Obstacle. It has a virtual method called onCollide().
I then have a child class called BouncePad, which overrides this method. For some reason, whenever I check for collisions, I first check if the object is of type Obstacle, and if it is, I call the obstacle.onCollide().

But for some reason, it does not seem to think of BouncePad as a type of obstacle, so it will not even collide with it. I am using XNA and C#. ANy ideas?



I think you're missing the whole point of polymorphism.
Declaring a method as virtual makes the link between the link between the class and method in runtime.
So you don't need to check of which type your object is. The correct method will be called automatically.


If you'll show us your code, we'll be able to see what you're doing wrong :)
[/quote]

The reason I check the type is because Obstacle is a child of a CollidableSprite, which inturn is a child of Sprite.
So, I am looping through my list of sprites on screen, and finding the obstacles. If the sprite is a typeof obstacle, then I cast it to an obstacle. But with my bouncepad, it doesnt get that far, it does not see it has a typeof obstacle, even though it has a "is a" relationship through inheritance.

And I do understand polymorphism, if I have many different obstacle objects all inheriting from the base obstacle class which as the onCollide method, I should be able to cast the sprite as an obstacle and call the onCollide, depending on what type of obstacle it was, the correct onCollide will be called, which is what I am trying to accomplish.
[/quote]

Ploymorphism = No casting required.
Polymorphism enables you to have an array of "Sprite"s, and run different implementations of the same interface without knowing exactly what types of "Sprite"s you're having.
The fact that you try to *cast* your sprite to an "obstacle" implies that you're doing something basically wrong.



and again, without seeing your code it will be pretty hard to know what is not working in it.
[/quote]

Mate, Of course I will need to cast it to a certain type. Child classes will more than likely have different methods than their parent, if you have a method that you want to call in a child class, you won't be able to call that method from its parent, it won't know what it is. The virtual method I was trying to invoke was in the Obstacle class, Meaning only an obstacle object could access it, meaning I needed a cast from my parent Sprite class. Thanks for your help anyway.
[/quote]

I think what he is saying is that you are misusing polymorphism quite heavily, if child classes have different methods than their parents then you should most likely store and deal with them separatly and reconsider if they really should inherit from that parent at all. (In your case it would make far more sense to store your Obstacles in their own list and give those a Sprite member rather than having them inherit from Sprite, Let your renderer keep a list of sprites and keep all graphical stuff away from the game logic).
[/quote]



I agree it would be much better practice to decouple things like that, but I am working on something that was already coded to a point where refactoring would take alot of time without any significant benefits.

Share this post


Link to post
Share on other sites

Mate, Of course I will need to cast it to a certain type.


No, mate, you wont. Not if you use polymorphism correctly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!