kira
kira

Reputation: 5

Condition ? : operator in C#

I have a problem with the ?: operator, so I tried the if-else block and everything works fine. But when using the other operator, it just stops working.

using System;

namespace firstGame
{
    class Program 
    {
        public string playerName;
        public int playerScore;
        public int gameNumber;
        public int playerGuess;

        public void GameStart()
        {
            string r;
            Console.WriteLine("Welcome to my first game");
            Console.Write("please enter your gamer name : ");

            this.playerName= Console.ReadLine();

            do
            {    
                this.gameNumber = Convert.ToInt16(new Random().Next(0,10));
                this.playerScore = 1;

                if (this.playerScore == 1)
                {
                    Console.WriteLine("Guess the hidden number between (1-10)");

                    do
                    {
                        Console.Write("guess number {0} :: ", this.playerScore);
                        string num = Console.ReadLine();

                        int.TryParse(num, out _) ? this.playerGuess=Convert.ToInt16(num) : Console.WriteLine("Are you sure it is a number !!?") ;
                        this.playerScore++;
                    } while (this.playerGuess != this.gameNumber);

                    Console.WriteLine("BINGO {0} is the correct number", this.gameNumber);
                    Console.Write("would you like a new lvl ? (Y/N) :: ");  r=Console.ReadLine();
                }
                else 
                { 
                    this.playerScore = 0; 
                    break; 
                }
            } while (r=="Y");
        }

        static void Main(string[] args)
        {
            new Program().GameStart();
        }
    }
}

I made a mistake somewhere, but where and how is beyond me.

Upvotes: 0

Views: 134

Answers (2)

TheGeneral
TheGeneral

Reputation: 81493

?: is not an if then else

The conditional operator

The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false

Moreover, Console.WriteLine does not return a result, it's a void method that has side effects. So it cannot be used with an operator, full stop.

In short, just use an if then else.

Upvotes: 0

Wyck
Wyck

Reputation: 11730

When using cond ? exp1 : exp2 the exp1 and exp2 must have the same type.

A ternary expression is not appropriate in your case because Console.WriteLine returns void but the assignment expression is an int.

Use the traditional if-else instead.

To fix

// ERROR: This doesn't compile
int.TryParse(num, out _) ? this.playerGuess=Convert.ToInt16(num) : Console.WriteLine("ar you sure it is a number !!?") ;

should become

if (int.TryParse(num, out _)) {
    this.playerGuess = Convert.ToInt16(num)
} else {
    Console.WriteLine("ar you sure it is a number !!?");
}

Bonus tip:

You probably actually want this:

if (int.TryParse(num, out int val)) {
    this.playerGuess = val;
} else {
    Console.WriteLine("Are you sure it is a number?");
}

Upvotes: 2

Related Questions