Problem with builder

Asked

Viewed 449 times

-1

This is the code of my class:

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


namespace Banco_Exercicio1
{
    public class Conta
    {
        public string Titular { get; set; }
        public double Saldo { get; set; }
        public int Numero { get; set; }

        public Conta(String nome)
        {
            Titular = nome;
        }

        public double Depositar(double Valor)
        {
            Saldo += Valor;
            return Saldo; 
        }

        public virtual void Saca(double Valor)
        {
            Saldo -= Valor;
        }
    }

    public class ContaPoupanca : Conta
    {
        public override void Saca(double Valor)
        {
            base.Saca(Valor + 0.10);    
        }      
    }
}

This is the code of my form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Banco_Exercicio1
{
    public partial class Form1 : Form
    {
        public Conta conta;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conta = new Conta ("Victor")
            {
               Numero = 1 , 
               Saldo = 0   
            };

            TBtextoTitular.Text = conta.Titular;
            TBtextoNumero.Text = Convert.ToString(conta.Numero);
            TBtextoSaldo.Text = Convert.ToString(conta.Saldo); 
        }

        private void deposito_Click(object sender, EventArgs e)
        {
            conta.Depositar(Convert.ToDouble(TBtextoValor.Text));
            TBtextoSaldo.Text = Convert.ToString(conta.Saldo); 
        }
    }
}

I’m making the following mistake:

Banco_exercicio1.Account does not contain a constructor that takes 0 Arguments"

How can I fix?

  • 2

    Even shared in the last link, post the content as the link may become disabled.

  • 2

    What a mistake you’re making?

  • @brasofilo - My fault. Corrected.

  • 1

    since ContaPoupanca inherits from Conta, it needs to have a constructor that gets at least the string name.

  • Managed to solve the problem?

1 answer

3

This mistake is happening because ContaPoupanca who inherits from the class Conta is implemented without constructor, which causes C# to generate a default constructor that calls the base constructor that has 0 arguments... only this constructor of the base class does not exist. There is only one who has 1 argument nome.

It is therefore necessary to take one of these measures:

  • add constructor with 0 argument in class Conta

    public class Conta
    {
        public Conta() { ...
    
  • add a constructor to the class ContaPoupanca manually, and call the constructor of the base class, passing a valid parameter

    public class ContaPoupanca
    {
        public ContaPoupanca(string nome)
            : base(nome)
        { ...
    

Browser other questions tagged

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