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.
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.
– Maniero