Why calling a method is not compiling?

0
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);
        }
    }
}
    
asked by anonymous 02.09.2018 / 15:34

2 answers

3

You have two problems: you are calling a method in another class, so you can only call it using the fully qualified name, hence the class name and method name, unless you use using to import . The other problem I preferred to solve this way is that this class and method should be static, even because they have no state, so you do not have 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");
    }
}

See working at .NET Fiddle . And no Coding Ground . Also put it in GitHub for future reference .

On the other hand, maybe I just wanted to put the method inside 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");
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
02.09.2018 / 16:37
2

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

Here 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 to define the Speak 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);
      }
  }
}
    
02.09.2018 / 16:46