1

I'm trying to make this class called 'Medico' that translates to Doctor, but in "this" it appears the error

Constructor 'Medico.Medico(string, string, int, string, string) cannot call itself.

I wanted to make a doctor class to add data in it, to a list view.

public class Medico
{
    public string Nome { get; set; }
    public string Especialidade { get; set; }

    private int _NIF;
    private DateTime _inicio;
    private DateTime _fim;

    public int NIF
    {
        get { return _NIF; }
        set
        {
            _NIF = value;
            if (_NIF < 99999999)
            {
                _NIF = 0;
            }
        }
    }

    public DateTime Inicio
    {
        get { return _inicio; }
        set
        {
            _inicio = value;
        }
    }

    public DateTime Fim
    {
        get { return _fim; }
        set
        {
            _fim = value;
        }
    }


    public Medico(string nome, string especialidade, int _NIF, DateTime _inicio, DateTime _fim)

    {
        Nome = nome;
        Especialidade = especialidade;
        NIF = _NIF;
        Inicio = _inicio;
        Fim = _fim;

    }

    public Medico(string nome, string especialidade, int _NIF, string _inicio, string _fim)
        : this(nome, especialidade, _NIF, _inicio, _fim) {            

    }

    public override string ToString()
    {
        return Inicio.ToString() + Fim.ToString();
    }
}  
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Simao Pina
  • 21
  • 1

3 Answers3

0

In your second constructor you are just passing the parameters you got.. so its calling itself.

this(nome, especialidade, _NIF, _inicio, _fim)

Instead cast the _inicio and _fim to a DateTime and it'll call the first constructor.

  • See Converting a String to a DateTime

    this(nome, especialidade, _NIF, DateTime.ParseExact(_inicio,"YourFormat"), 
         DateTime.ParseExact(_fim,"YourFormat"))
    
  • If you know the format is a correct DateTime format you can use also DateTime.Parse()

In general you might want to change and use object-initializers instead of having many constructors. More about when to use see:


In addition for your properties with the default implementation better use the auto-properties:

public DateTime Fim { get; set; }
public DateTime Inicio { get; set; }
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
0

If you want to create several constructors for Medico with different input parameters, just create a new medico constructor, without this:

public Medico(string nome, string especialidade, int _NIF, DateTime _inicio, DateTime _fim)
{
    Nome = nome;
    Especialidade = especialidade;
    NIF = _NIF;
    Inicio = _inicio;
    Fim = _fim;
}

public Medico(string nome, string especialidade, int _NIF, string _inicio, string _fim)
{
    // here convert _inicio and _fim to DateTime
    // And call the first constructor method with new parameters
    Medico(nome, especialidade, _NIF, inicioDateTime, fimDateTime);
}
pitersmx
  • 935
  • 8
  • 27
0

The following constructor definition:

public Medico(string nome, string especialidade, int _NIF, string _inicio, string _fim)
        : this(nome, especialidade, _NIF, _inicio, _fim) 

Means to call another constructor of this class and pass the values in this(...). Since you just pass the strings for _inicio and _fim the constructor tries to call itself. I suggest you want to call the other constructor:

public Medico(string nome, string especialidade, int _NIF, DateTime _inicio, DateTime _fim)

to do taht you would need to convert the strings to DateTimes first. Perhaps something like this:

public Medico(string nome, string especialidade, int _NIF, string _inicio, string _fim)
            : this(nome, especialidade, _NIF, DateTime.ParseExact(_inicio, 'yyyyMMdd',CultureInfo.InvariantCulture) , DateTime.ParseExact(_fim,'yyyyMMdd',CultureInfo.InvariantCulture)) 

Just change the pattern

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55