Implicit type conversion error

Asked

Viewed 134 times

0

Error 1 Cannot implicitly Convert type 'Cursocavancado.Client' to 'string' C: course Cursocavancado Cursocavancado Form1.Cs 47 31 Cursocavancado

Follows the class

namespace CursoCAvancado
{
    public partial class Form1 : Form
    {
        Conta conta;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void Sacar_Click(object sender, EventArgs e)
        {

                double valor = Convert.ToDouble(txtValorSaque.Text);
                this.conta.Saca(valor);
                if (valor < 100)
                {
                    MessageBox.Show("Saque efetuado com sucesso" + this.conta.Saldo);
                }
            }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.conta = new Conta();
            this.conta.Titular = new Cliente("everson");
            this.conta.Numero = 1;
            this.conta.Deposita(100);

            txtNumero.Text = Convert.ToString(this.conta.Numero);
            txtSaldo.Text = Convert.ToString(this.conta.Saldo);
            txtTitular.Text = this.conta.Titular;

        }
     }
}
classe conta
 class Conta
    {
        public int Numero { get; set; }
        public double Saldo { get; set; }
        public Cliente Titular { get; set; }

        public void Saca(double valor) 
        {
            this.Saldo -= valor;
        }
        public void Deposita(double valor) 
        {
            this.Saldo += valor;
        }
        public void Transfere(double valor, Conta destino) 
        {

        }
    }
}

Customer class

public class Cliente
{
    public string Nome { get; set; }

    public Cliente(string nome) 
    {
        this.Nome = nome;
    }
}
  • 2

    Everson, try to write a descriptive title and explain your problem and your code better. It’s just [Dit] the question. If you are interested, you have a guide: [Ask].

  • txtTitular.Text = this.conta.Titular;

  • I have done more yet so appears the error message, bfavaretto

  • What I mean is that the error is in this line. Holder is a client, txtTitular.Text is text. I think you want this.conta.Holder.Name

  • thanks @bfavaretto worked out that was it.

1 answer

2

The error cites line 47 of the file. I believe this is:

txtTitular.Text = this.conta.Titular;

If this.conta.Titular is a Cliente, you cannot directly assign to the value of a text field, which must be a string. To fill in the name of the holder, missed taking the appropriate property of his Cliente:

txtTitular.Text = this.conta.Titular.Nome;

Browser other questions tagged

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