DavMar
DavMar

Reputation: 75

Method from another class doesn't call

I am trying to call a method from another class. p.players() is supposed to open up when I choose this menu option:

 static void Main(string[] args)
    {

        Enumfactory.Position choice;
        Enumfactory.Location location;
        Player p = new Player();


        Console.WriteLine("Please choose from one of the following:");
        Console.WriteLine("1. GoalKeeper");
        Console.WriteLine("2. Defender");
        Console.WriteLine("3. Midfielder");
        Console.WriteLine("4. Striker");
        choice = ((Enumfactory.Position)(int.Parse(Console.ReadLine())));

        string exit = "";

        while (exit != "Y")
        {

            switch (choice)
            {
                case Enumfactory.Position.GoalKeeper:
                    //assigning the actual position
                    p.Position = Enumfactory.Position.GoalKeeper;
                    p.players();
                    break;

Here is my method from the class Player:

public string[] players()
    {
        List<string> PlayerList = new List<string>();
        Player player = new Player();
        string enterplayer = "";
        while (enterplayer == "Y")
        {
            Console.WriteLine("Please enter the teamnumber of your player");
            player.teamNumber = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the name of your player");
            player.name = Console.ReadLine();
            Console.WriteLine("Please enter the surname of your player");
            player.surname = Console.ReadLine();
            Console.WriteLine("Enter the age of your player");
            player.age = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the goals the player scored");
            player.goalsScored = int.Parse(Console.ReadLine());

            PlayerList.Add(player.teamNumber.ToString());
            PlayerList.Add(player.name);
            PlayerList.Add(player.surname);
            PlayerList.Add(player.age.ToString());
            PlayerList.Add(player.goalsScored.ToString());

            Console.WriteLine("Do you wish to enter another player? Y/N");
            enterplayer = Console.ReadLine();
        }
        foreach (var item in PlayerList)
        {
            Console.WriteLine("to view your player");
            Console.Write("{0}", item);
        }
        Console.ReadKey();
        return player.players();
    }

Upvotes: 0

Views: 105

Answers (2)

Jon G
Jon G

Reputation: 4164

The while loop in the way you've written it, will evaluate the condition before the first time it goes into the loop. You initialise enterplayer to "", so the first time the while condition is tested it returns false and never enters the loop. You can fix this in two ways, either by initialising enterplayer so that the condition is satisfied first time through:

string enterplayer = "Y";
while (enterplayer == "Y") // We set enterplayer to "Y" so this is true first time through
{
    // Your code to add a player goes here
}

...or, you can use a slightly different form of while loop, where the condition is evaluated at the end. This means that the code within the loop is always executed at least once, and then repeated as long as the while condition at the end is satisfied:

string enterplayer = "";
do // This always enters the loop code first time
{
    // Your code to add a player goes here
}
while (enterplayer == "Y")

Since your code is using the enterplayer variable to decide whether to add any more players, I'd prefer the second form, even though as a variant of the while construct it's probably less used than the former.

Upvotes: 0

computer10171
computer10171

Reputation: 445

The method is probably being called, it's just your while loop never runs. This is because enterplayer will never equal "Y", therefore the code in your while loop will never run (which makes it look like your method isn't being called).

Did you mean the following?

string enterplayer = "";
while (enterplayer != "Y")
{
    ...
}

Upvotes: 1

Related Questions