C # conversion error

0

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

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

I do not want the argument to count as type ContaCorrente , because other classes that inherit from the Account will have ITributável and another not.

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);
            }

        }

    }
}

TotalTymbols:

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();
        }

    }
}

Taxable:

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

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

    
asked by anonymous 03.11.2017 / 00:15

1 answer

1

You have not posted the class Conta , but you can tell that it does not implement ITributavel , so you can not use an object of type Conta in a parameter that expects a ITributavel . >

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

It's not the case, but I think this modeling has other conceptual problems. And it has other problems in the code, even if it does not give an error.

    
03.11.2017 / 00:32