C# conversion error

Asked

Viewed 138 times

0

I have the most generic class Account and another ContaPoupaça who inherits from that class Conta. In this daughter I have an interface called ITributavel, because this class beyond the methods and attributes of Conta, has a method of ITributavel.

I have another class TotalDeTributos that has a method that does an operation with the class ContaPoupanca who has the ITributável. In the method AdicionaConta, of the photo below, I put as argument account of the type Conta and is giving this error, I have tried to use casting to convert novaConta but makes another mistake.

I don’t want the script to count as ContaCorrente, because other classes that inherit from Account will have the ITributável and another no.

What am I doing wrong?

Form1:

using System;
using System.Windows.Forms;

namespace Banco2
{
    public partial class Form1 : Form
    {

        int indice1 = 0;
        int indice2 = 0;
        private int indiceNovaConta = 0;
        string tipoConta = "";

        private Conta[] contas = new Conta[10];
        TotalDeTributos tributos;
        ContaPoupanca c1;
        ContaInvestimento c2;
        Conta selecionada;



        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            c1 = new ContaPoupanca();
            c1.Titular = new Cliente("Rodrigo");
            c1.Numero = 1;
            this.AdicionarConta(c1);



            c2 = new ContaInvestimento();
            c2.Titular = new Cliente("Diego");
            c2.Numero = 2;
            this.AdicionarConta(c2);


            tributos = new TotalDeTributos();


            AtualizaTributo();
            lblValor.Text = "0";
        }

        private void btnDeposita_Click(object sender, EventArgs e)
        {
            double valorAcao = Convert.ToDouble(lblValor.Text);

            contas[indice1].Deposita(valorAcao);
            MessageBox.Show("Deposito realizado.");
            txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
            lblValor.Text = "0";
            AtualizaTributo();

        }

        private void btnSaque_Click(object sender, EventArgs e)
        {
            double valorOperacao = Convert.ToDouble(lblValor.Text);

            contas[indice1].Saque(valorOperacao);

            txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
            lblValor.Text = "0";

        }

        private void btnTransferir_Click(object sender, EventArgs e)
        {
            double valorOperacao = Convert.ToDouble(lblValor.Text);
            contas[indice1].Transfere(valorOperacao, contas[indice2]);

            txtSaldo2.Text = Convert.ToString(contas[indice2].Saldo);
            txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
            lblValor.Text = "0";

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            indice1 = comboBox1.SelectedIndex;
            selecionada = this.contas[indice1];

            txtTitular.Text = selecionada.Titular.Nome;
            txtSaldo.Text = Convert.ToString(selecionada.Saldo);
            txtNumConta.Text = Convert.ToString(selecionada.Numero);

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            indice2 = comboBox2.SelectedIndex;
            selecionada = this.contas[indice2];

            txtTitular2.Text = selecionada.Titular.Nome;
            txtSaldo2.Text = Convert.ToString(selecionada.Saldo);
            txtNumConta2.Text = Convert.ToString(selecionada.Numero);

        }

        public void AdicionarConta(Conta novaConta)
        {


            if (novaConta is ContaPoupanca)
            {
                this.tipoConta = "Conta Poupança";
            }
            else if (novaConta is ContaCorrente)
            {
                this.tipoConta = "Conta Corrente";
            }

            this.contas[this.indiceNovaConta] = novaConta;
            Conta selecionada = this.contas[this.indiceNovaConta];
            comboBox1.Items.Add(selecionada.Titular.Nome + " - Tipo: " + this.tipoConta);
            comboBox2.Items.Add(selecionada.Titular.Nome + " - Tipo: " + this.tipoConta);

            if(selecionada is ContaPoupanca)
            {
                MessageBox.Show(selecionada.GetType().ToString());
                tributos.Acumula((ITributavel)selecionada);
            }

            indiceNovaConta++;

        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            FormCadastrarConta formCadastrarConta = new FormCadastrarConta(this);
            int indiceUltimaConta = contas.Length;
            formCadastrarConta.ShowDialog();
        }

        public void AtualizaTributo()
        {

            if (tributos != null)
            {
                foreach(Conta conta in contas)
                {
                    if(conta is ContaPoupanca || conta is ContaInvestimento)
                    {
                        tributos.Acumula((ITributavel)conta);
                    }
                }


                txtTributos.Text = Convert.ToString(tributos.Total);
            }

        }

    }
}

Totaldetributos:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Banco2
{
    class TotalDeTributos
    {
        public double Total { get; private set; }

        public void Acumula(ITributavel conta)
        {
            this.Total += conta.CalcularTributo();
        }

    }
}

Itributavel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Banco2
{
    interface ITributavel
    {
        double CalcularTributo();
    }
}

Print do erro

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

  • Try catch helps a lot

2 answers

2

You didn’t post the class Conta, but you can already tell that it does not implement ITributavel, so there’s no way to use an object like Conta in a parameter that expects a ITributavel.

If you cannot put this interface on Conta, then you have to create an object of the type ContaPoupanca and not Conta to pass as argument. You can even create an object based on novaConta if it is found that the object is a ContaPoupanca. If you are using C# 7 you can even use Pattern matching.

That’s not the case, but I think this modeling has other conceital problems. And there are other problems in the code, even if it doesn’t make a mistake.

  • I’m following a workbook, but I always like to implement something else to test the knowledge acquired. It says Bill is the most generic class. Some classes that inherit from Account will have tributes and others will not, so I’m trying to pass something like Account in the argument. It is said in the booklet that using Interface would solve this problem. And if possible tell me which error to fix. I want to learn to do it right.

  • It’s a lot..

  • Jeez :/ What you recommend studying to get it right?

  • A lot of things ;) .

1

Rodrigo,

I think the concept of interface is getting in your way. See the interface ITributavel as a contract that must be fulfilled. Thus, in his method accumulates, he expects an object that fulfills the contract.

To fulfill this contract you need to decorate the class that will be instantiated and passed on to the method as object with the interface ITributavel and fulfill the contract, that is, implement in the class the method double CalcularTributo();.

It would be something like:

public class Conta : ITributavel {

   public double CalcularTributo() {
       // implementar regras de negócio
       return 0;
   }

}

This way you are ensuring that the passed object has the method CalcularTributo() and so you will be able to run the line this.Total += conta.CalcularTributo() - since he does not know that you have passed an Account or Savings Account, but knows that the object you have passed fulfills the contract ITributavel and so has the expected method.

I haven’t analyzed the rest of the code, but I think the central point is the fulfillment of this agreement established through the interface.

Browser other questions tagged

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