Jump to content
  • Advertisement
Sign in to follow this  
Andy474

[C#] Inheritance

This topic is 2720 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

Hey there, this may be a futile question.

I am creating a screen manager and i want to hold a Dictionary of Screens(a class)


public class ScreenManager
{
private Dictionary<string, Screen> _screenList;




public Screen GetScreen(string ID) { return _screenList[ID];}
}


Now, this works fine ofc. however the idea is you can create an inherited class such as your game screen.


public abstract class Screen {

}

public class GameScreen : Screen {
//Other Members
}


And I can add this to my screen manager <sManager.AddScreen("GameScreen", gamescreen);>
however i cannot then access any members from GameScreen when i call GetScreen("GameScreen");

Is there a way todo this without having to hold specific data members in the Screen manager class? ... if this doesnt sound right, can i hold it as a screen without having to create another Dict<string, GameScreen> ??

Share this post


Link to post
Share on other sites
Advertisement
You would need to cast the screen that is returned to GameScreen.

ie. GameScreen g_s = (GameScreen)sceenManager.GetScreen(id);

theTroll

Share this post


Link to post
Share on other sites
You simply need to cast to the more specific type, i.e.

GameScreen gs = (GameScreen)GetScreen("GameScreen");

However, I think you may have a design issue if you need to access the specific members from GameScreen when your manager is storing the more generic Screen. Why do you have to do this? (My initial assumption would be that there should be a method in both the Screen and GameScreen classes that has the same signature but is implemented differently according to the needs of both).


You may also find indexers and the as operator interesting reading.

Share this post


Link to post
Share on other sites
Thanks for the Quick reply :)

That worked if i simplally cast type it it works.

Basically, my Screen manager holds lots of "Screens" which could be Menus, Game Screens etc. Each Screen contains a copy of the ScreenManager,.
The reason i realised and asked this question is i wanted a button on the main menu which reset the game by calling GameScreen.DoReset().

I may also want to get things from the options screen, e.g. OptionsMenu.GetUserResalution(); however ofc it is stored as a "Screen"

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!