Installing a class as being owned by another class



  • 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 to use Especialidades inside Profissionais?

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

    Below is how I am 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"]);



  • This line:

    public Especialidades especialidade { get; private set; }
    

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

    Besides, it is not creating an object to use there, if you do not wish to create on your own you can do.

    profissional.especialidade = new Especialidades();
    

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

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

    https://github.com/maniero/SOpt/blob/master/CSharp/Property/Instancing.cs .

    I see other problems in the general conception of these objects, but it is not the focus of the question and I have lost people's hopes to do right. Either way, it'll work. You can search about https://pt.stackoverflow.com/q/73530/101 and https://pt.stackoverflow.com/q/212300/101 . Also https://pt.stackoverflow.com/q/354926/101 .




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2