Why is calling a method not compiling?

Asked

Viewed 100 times

-1

using System;
using System.IO;

namespace testando
{
    public class pessoa
    {
        public void Falar(){
            Console.WriteLine("Ola meu nome é ninguem");
        }
    }
    class Program
    {
        public static void Main(string[] args){
            Console.WriteLine("Escreva 1 para paresentar ola na tela ou so enter para sair");
            string r = Console.ReadLine();
            if(r == "1") {
                Falar();
            }
            Console.ReadKey(true);
        }
    }
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

There are two problems: you are calling a method in another class, so you can only call it using the full qualified name, so the class name and the method name, unless you use the using to import. The other problem I preferred to solve like this is that this class and method should be static, because they have no state, so you don’t need to instantiate anything. In a more organized way it would look like this:

using static System.Console;

namespace testando {
    class Program {
        public static void Main() {
            WriteLine("Escreva 1 para apresentar 'olá' na tela ou só <enter> para sair");
            if (ReadLine() == "1") Pessoa.Falar();
        }
    }
    public class Pessoa {
        public static void Falar() => WriteLine("Olá meu nome é ninguém");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

On the other hand maybe I just wanted to put the method within the same class, then I could call the method directly:

using static System.Console;

namespace testando {
    class Program {
        public static void Main() {
            WriteLine("Escreva 1 para apresentar 'olá' na tela ou só <enter> para sair");
            if (ReadLine() == "1") Falar();
        }
        public static void Falar() => WriteLine("Olá meu nome é ninguém");
    }
}

Behold working in the idepne. And in the .NET Fiddle. Also put on the Github for future reference.

3

The Speak method is found in another class, so that it is accessed through the Main method found in the Program class, the Person class must be instantiated and then call the Speak method, through it.

Below is an example:

using System;
using System.IO;

namespace testando
{
  public class pessoa
  {
    public void Falar()
    {
        Console.WriteLine("Ola meu nome é ninguem");
    }
  }
  class Program
  {
    public static void Main(string[] args)
    {
        Console.WriteLine("Escreva 1 para paresentar ola na tela ou so enter 
        para sair");
        string r = Console.ReadLine();
        if (r == "1")
        {
            pessoa pess = new pessoa();
            pess.Falar();
        }
        Console.ReadKey(true);
    }
  }
}

Another solution would be defining the Talk method as static, so it would not be necessary to instantiate the class:

using System;
using System.IO;

namespace testando
{
   public class pessoa
   {
      public static void Falar()
      {
        Console.WriteLine("Ola meu nome é ninguem");
      }
   }
   class Program
   {
      public static void Main(string[] args)
      {
        Console.WriteLine("Escreva 1 para paresentar ola na tela ou so enter 
            para sair");
        string r = Console.ReadLine();
        if (r == "1")
        {
            pessoa.Falar();
        }
        Console.ReadKey(true);
      }
  }
}

Browser other questions tagged

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