Instantiate class as another class’s Property

Asked

Viewed 4,106 times

4

I have two classes created within the same namespace, being:

public class Pessoa
{
    public int idPessoa { get; set;}
    public string nome { get; set;}
}

public class PessoaFisica
{
    public Pessoa pessoa { get; set;}
    public string cpf { get; set; }
    public string rg { get; set; }
}

But when I try to use class PessoaFisica, get the error message:

Undefined object reference for an object instance.

How do I instantiate the class Pessoa within the class PessoaFisica without having to use the operator new?

The classes 'Person' and 'Personal', have the function of DTO. Consequently, they will be used by other classes of business rules to popularize the registration Datagrids of suppliers, customers and etc.

4 answers

2

Can instantiate a new Person in the Personal constructor:

public PessoaFisica()
{
    pessoa = new Pessoa();
}

If you do not want the Personal Usersphysic to change the person property, you can change the Setter to private.

public Pessoa pessoa {get; private set; }

This ensures that the property can only be changed by you.

  • It worked, but working like this, Datagrid is filled but does not display the data...

  • There is no mention of a Datagrid in the question. Why don’t you update the question with the Datagrid question and explain to us the purpose of Datagrid and its connection to People?

  • @Omni, I updated the question as you suggest. Help?

  • @user8433 yes help. As the Crood asked, the physical person fields were populated? You have to take into account that by default string type properties are null, that is, in Datagrid the fields will appear empty. Try giving a value to Cpf and rg when creating the list populating Datagrid and see if the data values appear.

  • @Omni, I will proceed as you suggest and as soon as I have the result, put here. For now thank you.

  • @user8433 one more suggestion, if you try and still can’t, edit the question and put the code in which you assign the Datagrid datasource.

Show 1 more comment

1

The most recommended thing in this situation is to create an inheritance of a person in a natural person.

Something like:

public class PessoaFisica : Pessoa

But if you want to work without inheritance you need to instantiate the person class in the physical person builder.

In your person class add a constructor method.

 public PessoaFisica()
 {
     pessoa  = new Pessoa();     
 }
  • I instated the class Person within the class Personal Physics and it worked... the only problem is that when defining the Datasource of my Datagrid the lines are filled but without appearing the values.

  • @Crood, yes I did.

1

You will have to instantiate the Person class, you can do this on GET.

 public class Pessoa {
    public int idPessoa { get; set; }
    public string nome { get; set; }
}

public class PessoaFisica {
    private Pessoa pessoa;
    public Pessoa Pessoa {
        get {
            if (pessoa == null)
                pessoa = new Pessoa();

            return pessoa;
        }
    }
    public string cpf { get; set; }
    public string rg { get; set; }
}
  • I tried to do as you suggested, but that way the problem persists.

1

Example of encoding when Pessoa is aggregated to PessoaFisica.

Normalizing

public class Pessoa
{
    public int IdPessoa { get; set; }
    public string Nome { get; set; }
}

public class PessoaFisica
{
    public PessoaFisica()
    {
        //INSTANCIANDO NO CONTRUTOR
        this.pessoa = new Pessoa();
    }
    private Pessoa pessoa;
    public Pessoa Pessoa
    {
        get { return pessoa; }
        set { pessoa = value; }
    }        
    public string Cpf { get; set; }
    public string Rg { get; set; }
}

Creating a list of PessoaFisica

IList<PessoaFisica> PessoasListaFisica = new List<PessoaFisica>();

PessoasListaFisica.Add(new PessoaFisica()
{
    Pessoa = new Pessoa() { IdPessoa = 1, Nome = "Pessoa1" },
    Cpf ="12345678900", 
    Rg = "147852369SSP"
});
PessoasListaFisica.Add(new PessoaFisica()
{
    Pessoa = new Pessoa() { IdPessoa = 2, Nome = "Pessoa2" },
    Cpf ="00987654321", 
    Rg = "963258741SSP"
});

Using the list of PessoasFisica in the DataGridView

dataGridView1.DataSource = PessoasListaFisica.Select(s => new
{
    s.Rg, 
    s.Cpf, 
    s.Pessoa.IdPessoa, 
    s.Pessoa.Nome
}).ToArray();

Upshot:

inserir a descrição da imagem aqui

  • so test your example, put the result. For now thank you very much for the help.

  • I tried using your example but am getting this warning message 'Method must have a Return type'. The Personal Public Method() { //INSTANTIATING NO CONTRUTOR this.pessoa = new Person(); } is the same void type?

  • 1

    @user8433 the method referred to is the Personal constructor(), has no return type.

  • There is no return, it is a constructor method, that is, it will be executed in the unstable class Pessoafisica();. If you are doubtful in the example ?

Browser other questions tagged

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