Amer
Amer

Reputation: 81

Calling a variable from child class in parent class in c#

I am trying to raed the value of userInput variable in my parent class which is stored in my child class, but I can't reach it and I get many errors one after one after each try. Can you please help me?

This is apart of my code:

//Child class
class tictactoe
{
     public void beginGame()
     {
        ConsoleKeyInfo gebruikerInvoer;
        gebruikerInvoer = Console.ReadKey();
        //Rest of code
      }
}

//Parent class
namespace Quizz
{
    class Program
    {
        public static void Main(string[] args)
        {
            tictactoe minigame1 = new tictactoe();
            while (minigame1.gebruikerInvoer)
            {
                //Rest of code
            }
        }
    }
}

This is the error I get

'tictactoe' does not contain a definition for 'gebruikerInvoer' and no accessible extension method 'gebruikerInvoer' accepting a first argument of type 'tictactoe' could be found (are you missing a using directive or an assembly reference?)

I think that I will need to make a method to call it from the parent, if so: what type should I give since the variable is a ConsoleKeyInfo that is later converted to string?

gebruikerInvoer.KeyChar.ToString()

Upvotes: 0

Views: 1074

Answers (1)

ExplodatedFaces
ExplodatedFaces

Reputation: 431

As the comments have made apparent, you need to first make the variable visible to the outside world. Local variables are always hidden from other classes. So lets fix this first:

    class tictactoe
    {
        public ConsoleKeyInfo gebruikerInvoer; //Make public and move owner to class not method
        public void beginGame()
        {
            gebruikerInvoer = Console.ReadKey();
            //Rest of code
        }
    }

    //Parent class
    namespace Quizz
    {
        class Program
        {
            public static void Main(string[] args)
            {
                tictactoe minigame1 = new tictactoe();
                while (minigame1.gebruikerInvoer) //CS0029 ConsoleKeyInfo cannot be implicitly converted to type Bool
                {
                    //Rest of code
                }
            }
        }
    }

Moving on from there you need to define a statement to test the variable's value in a way that can return a true or false statement to use a while loop. To fix that we can do something like this:

    class tictactoe
    {
        public ConsoleKeyInfo gebruikerInvoer;
        public void beginGame()
        {
            gebruikerInvoer = Console.ReadKey();
            //Rest of code
        }
    }

    //Parent class
    namespace Quizz
    {
        class Program
        {
            public static void Main(string[] args)
            {
                tictactoe minigame1 = new tictactoe();
                bool minigameRun = minigame1.gebruikerInvoer.KeyChar == 't' 
                    ? true : false; //Assign a true value if the user entered the letter 't', else false
                while (minigameRun) //Runs rest of code while minigameRun is true
                {
                    //Rest of code
                    
                }
            }
        }
    }

Then to escape the while loop you can use return, break, or upon some condition change the local bool minigameRun to false. The bool is initially assigned in this code using the ?: operator. You can read more about its use here

Upvotes: 1

Related Questions