Error #CS0176 in if statement, what am I doing wrong?

Asked

Viewed 143 times

3

Why am I getting error #CS0176 when I call variable madeiras in (gamble.Equals("gamble {0}", madeiras))?

From what I understand is because my method is static, and I need to put the name of your class, but the error persists if I put (gamble.Equals("gamble {0}", Program.madeiras)), for example.

 namespace GambleGame
 {
    class Program
    {   
        //váriaveis em int
        static int madeiras = 0;

        //váriaveis em string
        static string gamble;


        static void Main(string[] args)
        {
            //método principal
            //...
            //Gamble Game
            //cor da intro
            Console.ForegroundColor = ConsoleColor.Green; 
            
            //executa o método estático "intro"
            intro();
        }

        public static void intro()
        {
            //código do método estático intro
            Console.WriteLine("---------------------------|||Seja bem vindo ao Gamble Game|||---------------------------");
           
            {  
                //cor das intruções
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Yellow;
            }

            Console.WriteLine("- Intruções:");     
            Console.WriteLine("- Digite ''gamble'' para farmar 10 madeiras");
            Console.WriteLine("- Para apostar ''X'' madeiras, digite ''apostar'' e o número de madeiras");
            Console.WriteLine("- Você poderá apostar ''X'' madeiras para ganhar a mesma quantidade");
            Console.WriteLine("- Porém, se você apostar ''X'' madeiras e perder, você perde essa mesma quantidade");
            Console.WriteLine("- Tente ser o maior lenhador dentre seus amigos, boa sorte !");
            Console.WriteLine("- Aperte Enter para começar !");
            Console.ReadLine();
            Console.Clear();
            start(); //executa o método estático "start"
        }

        public static void start()
        {
            {
                //cor do start
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Cyan;
            }

            // loop for(int loop = 0; loop < 999*999; loop++)
            //código do método start
            Console.WriteLine("Digite ''gamble'' para bater na árvore");
            Console.WriteLine("Você possui {0} madeiras", madeiras);

            bool loop = true;

            while (loop)
            {
                gamble = Console.ReadLine();

                if (gamble.Equals("gamble"))
                {
                    madeiras = madeiras + 10;
                    Console.Clear();
                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("Digite ''gamble'' para bater na árvore");
                }

                if (gamble.Equals("gamble {0}", madeiras))
                {
                    madeiras = madeiras + madeiras;
                }
                Console.WriteLine("Você possui {0} madeiras", madeiras);
            }           
        }
    }
}
  • complete the code with the declaration of the methods

  • 1

    What do you want to do with "gamble {0}", madeiras inside the Gamble. Equals()?

  • The complete code has been placed. Correct, extension.

  • I think maybe the point was to do gamble.Equals(string.Format("gamble {0}", madeiras)).

  • Correct, John, it is already working now, I understand the error. Thanks for the help

2 answers

4


The error is due to the fact that the String class has a static method with that signature(public Static bool Equals(string,string)) and being qualified with an instance name.

To use it you must qualify it with the class name:

String.Equals("gamble {0}", madeiras);

This will compare the string "gamble {0}" with the contents of the variable madeiras.

From what is given to understand is not what you intend, but yes:

gamble.Equals(string.Format("gamble {0}", madeiras))

or, better yet, using "string interpolation":

gamble.Equals($"Texto {madeiras}")
  • No @ramaral, the problem is that string interpolation is incorrect, you lack the string.Format.

  • @Joãomartins I know, see my comment. However I am responding to what was asked: Error #CS0176 in if statement, what I’m doing wrong?

4

At first I see that the loop is infinite, even uses a variable without need. You need to see how you will get out of it, I doubt that this is the intention.

It’s not a mistake, but use Equals() in C# makes no sense (there may be a very specific context). Other things can be modernized. The comments are quite unnecessary, add zero useful information. The code is very confusing and does not use C#’s nomenclature standard. And does not validate and error treatments.

Static variables do not make sense because they are only used locally.

And mostly there are parts that don’t make sense.

The mistake is documented:

Static Member 'Member' cannot be accessed with an instance Reference; qualify it with a type name Instead

I improved and arrived at it, but it’s still weird and I don’t know if it’s what you want, the question doesn’t describe the problem properly, I just figured out a way to compile and then see the other mistakes:

using static System.ConsoleColor;
using static System.Console;

namespace GambleGame {
    public class Program {
        public static void Main(string[] args) {
            ForegroundColor = Green;
            Intro();
        }

        public static void Intro() {
            //código do método estático intro
            WriteLine("---------------------------|||Seja bem vindo ao Gamble Game|||---------------------------");
            ResetColor();
            ForegroundColor = Yellow;
            WriteLine("- Intruções:");
            WriteLine("- Digite ''gamble'' para farmar 10 madeiras");
            WriteLine("- Para apostar ''X'' madeiras, digite ''apostar'' e o número de madeiras");
            WriteLine("- Você poderá apostar ''X'' madeiras para ganhar a mesma quantidade");
            WriteLine("- Porém, se você apostar ''X'' madeiras e perder, você perde essa mesma quantidade");
            WriteLine("- Tente ser o maior lenhador dentre seus amigos, boa sorte !");
            WriteLine("- Aperte Enter para começar !");
            ReadLine();
            Clear();
            Start(); //executa o método estático "start"
        }

        public static void Start() {
            ResetColor();
            ForegroundColor = Cyan;
            int madeiras = 0;
            WriteLine("Digite ''gamble'' para bater na árvore");
            WriteLine($"Você possui {madeiras} madeiras");
            while (true) {
                var gamble = ReadLine();
                if (gamble == "gamble") {
                    madeiras += 10;
                    Clear();
                } else {
                    Clear();
                    WriteLine("Digite ''gamble'' para bater na árvore");
                }
                if (gamble == $"gamble {madeiras}") {
                    madeiras += madeiras;
                }
                WriteLine($"Você possui {madeiras} madeiras");
            }
        }           
    }
}

I put in the Github for future reference.

  • Yes, I am aware of the infinite loop, but that is the intention, I am making a game that is basically a loop with various conditions, therefore, I do not intend to leave the loop until I give the condition to Win or lose the game.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.