Instantiating a class as being property of another class

Asked

Viewed 143 times

-2

I have the classes:

using System;

namespace Model
{
    public class Profissionais
    {
        public int idProfissional { get; set; }
        public String Nome { get; set; }
        public String CPF { get; set; }
        public DateTime DataNascimento { get; set; }
        public String Sexo { get; set; }
        public String Email { get; set; }
        public int CodEspecialidade { get; set; }
        public String Observacoes { get; set; }
        public String Endereco { get; set; }
        public String NumeroEnd { get; set; }
        public String CEP { get; set; }
        public String Bairro { get; set; }
        public String Cidade { get; set; }
        public String UF { get; set; }
        public String Telefone { get; set; }
        public String Celular { get; set; }
        public bool Ativo { get; set; }
        public Especialidades especialidade { get; private set; }
    }
}

and

using System;

namespace Model
{
    public class Especialidades
    {
        public int idEspecialidade { get; set; }
        public string descricao { get; set; }
        public Boolean ativo { get; set; }
    }
}

How I use Especialidades inside Profissionais?

Check that I’ve already created the property of the type Especialidade, but when I add a value to it, instantiating the object Profissionais and says that the object is not instantiated.

Below how I’m getting the value:

 Profissionais profissional = new Profissionais();

 profissional.idProfissional = Convert.ToInt32(dataReader["idProfissional"]);
 profissional.especialidade.idEspecialidade = Convert.ToInt32(dataReader["codEspecialidade"]);
 **profissional.especialidade.descricao = Convert.ToString(dataReader["especialidade"]);**
 profissional.Nome = Convert.ToString(dataReader["nome"]);

2 answers

1

This line:

public Especialidades especialidade { get; private set; }

Clearly shows that you can only write on the property (set) privately (private), so if you want to write in it publicly you have to take that visibility attribute out of the property and leave it to others.

Also you are not creating an object to use there, if you do not want to create on your own you can do.

profissional.especialidade = new Especialidades();

But it can keep the private field if initializing within the class could do so:

public Especialidades especialidade { get; private set; } = new Especialidades();

I see other problems in the general conception of these objects, but it is not the focus of the question and I have lost the hopes of people doing right. Either way it will "work". You can search about builder and abuse of getter/Setter. Also abstraction leak.

  • When you set the value give me this: Message "Exception of type 'System.Stackoverflowexception' was thrown."

  • There is another problem and algorithm in this code shown that does not occur. I answered upon the posted. Ask a new question about this.

  • Is calling functions recursively?

0

When you instantiate an object Profissionais it does not automatically create the instances of the fields, there are several ways to do this:

Constructively

using System;

namespace Model
{
    public class Profissionais
    {
        public int idProfissional { get; set; }
        public String Nome { get; set; }
        public String CPF { get; set; }
        public DateTime DataNascimento { get; set; }
        public String Sexo { get; set; }
        public String Email { get; set; }
        public int CodEspecialidade { get; set; }
        public String Observacoes { get; set; }
        public String Endereco { get; set; }
        public String NumeroEnd { get; set; }
        public String CEP { get; set; }
        public String Bairro { get; set; }
        public String Cidade { get; set; }
        public String UF { get; set; }
        public String Telefone { get; set; }
        public String Celular { get; set; }
        public bool Ativo { get; set; }
        public Especialidades especialidade { get; private set; }
        public Profissionais()
        {
            especialidade = new Especialidades();
        }
    }
}

Assigning an instance

Profissionais profissional = new Profissionais();
profissional.idProfissional = Convert.ToInt32(dataReader["idProfissional"]);
profissional.especialidade = new Especialidades();
profissional.especialidade.idEspecialidade = Convert.ToInt32(dataReader["codEspecialidade"]);
profissional.especialidade.descricao = Convert.ToString(dataReader["especialidade"]);
profissional.Nome = Convert.ToString(dataReader["nome"]);

In short, every field that is object of type Referência must be instantiated in some way. The property is associated with an implicit field.

There is no right way to do this, but surely there is a more efficient way.

You should consider not exposing objects as properties (get and set) in a component that will publish or sell.

Browser other questions tagged

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