System Error.Indexoutofrangeexception

Asked

Viewed 1,545 times

5

I’m having mistakes in Titular.Text of this form and do not know how to solve.

public partial class Titular : Form
    {
        private Conta[] contas;
        private int quantidadeDeContas;
        public Titular()
        {
            InitializeComponent();
        }

        private void Titular_Load(object sender, EventArgs e)
        {
            this.contas = new ContaCorrente[30]; 

            this.contas[0] = new ContaCorrente();
            contas[0].Numero = 1; contas[0].Nome = "Conta Corrente";
            contas[0].Titular = new Cliente();
            contas[0].Titular.Nome = "Vitor";
            contas[0].Saldo = 100;

            contas[1] = new ContaPoupanca();
            contas[1].Numero = 2;
            contas[1].Titular = new Cliente();
            contas[1].Titular.Nome = "Mario"; contas[1].Nome = "Conta Poupanca";
            this.quantidadeDeContas =30;
            foreach (Conta conta in contas)
            {
                if (conta != null)
                    comboContas.Items.Add(conta.Titular.Nome);
            }
            comboContas.DisplayMember = "Titular";
        }

        private void comboContas_SelectedIndexChanged(object sender, EventArgs e)
        {
            string titularSelecionado = comboContas.Text; 

            int indiceSelecionado = comboContas.SelectedIndex;
            Conta contaSelecionada = contas[indiceSelecionado];
            txtTitular.Text = Convert.ToString(contaSelecionada.Nome);
            txtNumero.Text = Convert.ToString(contaSelecionada.Numero);
            txtSaldo.Text = Convert.ToString(contaSelecionada.Saldo);

        }

        private Conta BuscaContaSelecionada()
        {
            int indiceSelecionado = comboContas.SelectedIndex;
            return this.contas[indiceSelecionado];
        }
        private void btnSaca_Click(object sender, EventArgs e)
        {
            string txtValorSaque = txtValor.Text;
            Conta contaSelecionada = this.BuscaContaSelecionada();

            contaSelecionada.Saca(Convert.ToDouble(txtValor.Text));
        }

        private void btnDeposita_Click(object sender, EventArgs e)
        {
            string txtValorDoDeposito = txtValor.Text;
            Conta contaSelecionada = this.BuscaContaSelecionada();
            contaSelecionada.Deposita(Convert.ToDouble(txtValor.Text));
        }

        private void btnTransferir_Click(object sender, EventArgs e)
        {
            Conta conta1 = BuscaContaSelecionada();
            int indiceSelecionado = cmbDestinoDaTransferencia.SelectedIndex;
            Conta conta2 = this.contas[indiceSelecionado];

            conta1.Transfere(Convert.ToDouble(txtValor.Text), conta2);
            txtTitular.Text = conta2.Titular.Nome;
            txtNumero.Text = conta2.Numero.ToString();
            txtSaldo.Text = conta2.Saldo.ToString();
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            txtTitular.Clear();
            txtNumero.Clear();
            txtSaldo.Clear();
            txtValor.Clear();
        }

        private void cmbDestinoDaTransferencia_SelectedIndexChanged(object sender, EventArgs e)
        {
            int indiceSelecionado = cmbDestinoDaTransferencia.SelectedIndex;
            Conta contaSelecionada = this.contas[indiceSelecionado];

            txtTitular.Text = contaSelecionada.Titular.Nome;
            txtNumero.Text = contaSelecionada.Numero.ToString();
            txtSaldo.Text = contaSelecionada.Saldo.ToString();
        }

        public void AdicionaConta(Conta conta)
        {
           if (quantidadeDeContas == contas.Length)
            {
                Conta[] temporario = new Conta[contas.Length * 2];
                for (int i = 0; i < this.contas.Length; i++)
                {
                    temporario[i] = this.contas[i];
                }
                this.contas = temporario;
            }
            this.contas[this.quantidadeDeContas] = conta;
            this.quantidadeDeContas++;
            comboContas.Items.Add(conta);
        }

        private void btnNovoCadastro_Click(object sender, EventArgs e)
        {
            CadastroDeConta cadastroDeContas = new CadastroDeConta(this);
            if (cadastroDeContas.ShowDialog() == DialogResult.OK            
            {
                Application.Run(new CadastroDeConta(this));            
            }
        }
  }
  • Where exactly is going wrong?

  • Put the ZIP with the Project to open here? AI we can see where the error is... It should not be something too tricky no.

  • It was important to know where exactly the error is, on which line, otherwise we will throw tips to chance.

1 answer

1

A Indexoutofrangeexception exception is cast when an invalid index is used to access a member of an array or a collection, or read or write a specific location in a buffer. This exception inherits the Exception class but adds non-exclusive members. Usually, a IndexOutOfRangeException exception is released as a result of developer errors. Instead of treating the exception, you should diagnose the cause of the error and fix your code.

You are probably trying to access an index of your objects that does not exist. Could be any one of those.

int indiceSelecionado = comboContas.SelectedIndex;
Conta contaSelecionada = contas[indiceSelecionado];

/

private Conta BuscaContaSelecionada()
        {
            int indiceSelecionado = comboContas.SelectedIndex;
            return this.contas[indiceSelecionado];
        }

/

Conta conta1 = BuscaContaSelecionada();
            int indiceSelecionado = cmbDestinoDaTransferencia.SelectedIndex;
            Conta conta2 = this.contas[indiceSelecionado];

/

nt indiceSelecionado = cmbDestinoDaTransferencia.SelectedIndex;
            Conta contaSelecionada = this.contas[indiceSelecionado];

Browser other questions tagged

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