Problem with constructor

-1

This is the code for 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 my form code:

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 have the following error:

  

Bank_Exercise1.Conta does not contain a constructor that takes 0 arguments "

How can I resolve this?

    
asked by anonymous 07.04.2014 / 19:53

1 answer

3

This error is happening because ContaPoupanca that inherits from 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 that has 1 argument nome .

In this way, you need to take one of these measures:

  • add constructor with 0 argument in class Conta

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

    public class ContaPoupanca
    {
        public ContaPoupanca(string nome)
            : base(nome)
        { ...
    
07.04.2014 / 20:28