Might look a silly question, but can I return an instance?
Example:
public class EcranJeu : AffichageJeu
{
public EcranJeu(string choixecran)
{
if (choixecran == "0")
{
KystExtract décor = new KystExtract();
}
if (choixecran == "1")
{
KystExtraction décor = new KystExtraction();
}
}
public override void LoadContent()
{
décor.LoadContent(content);
}
}
décor is said not to exist. How can I pass it to the LoadContent, Update and Draw of all the class?
The other answer was only partially correct as you found.
Problem: You are creating an object of one of two different classes and later want to call a
LoadContent(), orDraw()orUpdate()method on the chosen object.I have to assume the two classes
KystExtractandKystExtractionhave a common base class or share an interface. If not you will need to create/add one (e.g.KrystBaseor IKryst) that defines theLoadContent()method.The property you store is then of the base class type/interface, so it can hold either a
KystExtractobject or aKystExtractionobject.Your code will look like this (assuming you have no common base class and use an interface):