The name "Arthur" does not exist in the current context C#

Asked

Viewed 339 times

1

I am doing a test where I seek to get the Client information by Classemenu.

However, I’m having a hard time getting the information to the Program.

Client class

public class Cliente
{
    public string nome { get; internal set; }
    public string email { get; set; }
    public string dataNascimento { get; set; }
    public string cpf { get; set; }
    public string rg { get; set; }
    public string endereco { get; set; }
    public string senha { get; set; }
}

Menu Class

public class ClasseMenu
{
    public Cliente titular;
}

Program

class Program
{
    static void Main(string[] args)
    {
        ClasseMenu contaUm = new ClasseMenu();
        contaUm.titular = arthur;

        Console.WriteLine(contaUm.titular);

        Console.ReadLine();
    }
}
  • 2

    In your case, Arthur is a variable. You do not have such a variable declared. If you have what as text, you should enclose it with double quotes.

3 answers

4

It seems to me you’re trying to use the word arthur as a string. Well, literal strings need to be surrounded with double quotes.

contaUm.titular = "arthur";

But this will not leave your code correct. Because the property (field, in this case) titular should be an instance of Cliente.

Probably what you want to do is:

var cliente = new Cliente();
cliente.nome = "arthur";

// ... definição das outras propriedades

contaUm.titular = cliente;
  • 1

    The variable titular wouldn’t be a class like Cliente?

  • @It’s true, my friend.

  • This solution is good, but then it takes away the purpose of wanting to seek the Customer’s information by Classemenu.

1


First thing you could create a specialized constructor for the class Cliente and this constructor fills the fields nome, email, dataNascimento, cpf,rg, endereco and senha. Then overwrite the method Object.ToString() so that the method call Console.WriteLine(contaUm.titular.); becomes appropriate:

  public class Cliente
  {
    public string nome { get; internal set; }
    public string email { get; set; }
    public string dataNascimento { get; set; }
    public string cpf { get; set; }
    public string rg { get; set; }
    public string endereco { get; set; }
    public string senha { get; set; }

    // Todos os campo, exceto nome, possuem um valor na falta para evitar digitação 
    //desnecessária num exemplo.
    public Cliente(string nome, string email = "", string dataNascimento = "",
                   string cpf = "", string rg = "", string endereco"= "", string senha = "")
    {
        this.nome =nome;
        this.email = email;
        this.dataNascimento = dataNascimento;
        this.cpf = cpf;
        this.rg = rg;
        this.endereco = endereco;
        this.senha = senha;
    }

    // Sobrescrição do método Object.ToString()
    public override string ToString()
    {
       return = $"Titular: {nome}, email: {email}, Nascimento: {dataNascimento}, CPF: {cpf}, RG: {rg}, endereço: {endereco}, senha: {senha}"
    }
 }

So every time you want to create a client invoke the class constructor Cliente.

class Program
    {
        static void Main(string[] args)
        {

            // Cria um cliente só com o campo nome preenchido
            ClasseMenu contaUm = new ClasseMenu();
            contaUm.titular = new Cliente("Arthur");

            //Se quiser criar um cliente com todos os campos preenchido.
            ClasseMenu contaDois = new ClasseMenu();
            contaDois.titular = new Cliente("Jandira", "[email protected]", "21/08/1987", "956087565-23", "000.67.58.53", "Rua dos Coqueiros nº23", "987abc567" );

            Console.WriteLine(contaUm.titular.ToString());


            Console.WriteLine(contaDois.titular.ToString());


            Console.ReadLine();
        }
    }
  • 1

    Thanks for the help Augusto!

-1

As the above user said, the string in C# has to be inside (") double quotes to represent a conjunct with multiple characters. A tip is to use get and setters in your parameters to create an encapsulation of their properties.

In case the Get would be that your property it will return a value, the one or the other defined depending on the case.

And the Setters would be to introduce a value to its variable by placing method { set; } the value will be inserted directly into the property, but this concept is used so that you can control how the values will be inserted.

Using encapsulation is always a good option to not make your code vulnerable to modifications that may affect the functioning of the same.

public class ClasseMenu
{
    public Cliente titular {get;set;}
}

So you respect a very important concept in object-oriented programming. And of course you can define only a get or Setter.

Browser other questions tagged

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