Make the console take a value from another class in C#

Asked

Viewed 41 times

2

I’m trying to learn C-language. In this case I’m trying to get the Console to show the balance value by taking this value from a different class in the same file (I’ve tried it with multiple-files and it still doesn’t work, the compiler doesn’t see the variable).

using System;
using namespaceb;

namespace namespaceb
{
    public class saldo
    {
       public int numero = 100;
    }
}

namespace namespacea
{
    class Program
    {
        static void Main(string[] args)
        {
            saldo teste = new saldo();
            Console.WriteLine(numero); //dá erro "O nome Numero não existe no contexto atual"
            Console.WriteLine(teste); // mostra uma linha "namespaceb.saldo"
            Console.ReadKey();
        }
    }
}
  • 1

    If the class is in the same namespace you can use her saldo s = new saldo(); next s.numero

  • 2

    only missing Console.WriteLine(teste.numero);

1 answer

2

using System;
using namespaceb;

namespace namespaceb
{
    public class saldo
    {
       public int numero = 100;
    }

    class Program
    {
        static void Main(string[] args)
        {
            saldo teste = new saldo();
            Console.WriteLine(teste.numero);
            Console.ReadKey();
        }
    }
}

Browser other questions tagged

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