Failed to display fields of the Address class when using object-creation method expressed in C #

3

Visual Studio 2017 suggested something that surprised me, creating an object without using the new operator, I was surprised because I'm coming from Java and it's been a year since I was stopped and I decided to go back to school.

Well, when creating a new Enterprise object, it does not appear the fields of the Address class that are declared inside the Company class, but inside it also has the class Plan and this Yes, they appear to complete.

I can not see wrong, because the two classes are almost equal, taking away the fact that Plan receives inheritance of the class Persistant.

Below is the code to see the classes:

Business Class

namespace Cobranca.pkgModel
{
    public class Empresa : Persistant
    {
        private string nome;
        private string razaoSocial;
        private string cnpj;
        private string status;
        private Plano plano;
        private Endereco endereco;

        public Empresa()
        {
            Endereco = new Endereco();
            Plano = new Plano();
        }

        //métodos getter e setter
    }
}

Plan Class

namespace Cobranca.pkgModel
{

    public class Plano : Persistant 
    {
        private string nome;
        private string tipoCobranca;
        private double valor;

        //métodos getter e setter
    }
}

Class Address

namespace Cobranca.pkgModel
{
    public class Endereco
    {
        private string logradouro;
        private string numero;
        private string bairro;
        private string complemento;
        private string cep;
        private string cidade;
        private string estado;

        //métodos getter e setter
    }
}

Below is the print I took from the screen

SomeoneknowstoanswermebecauseI'mgoingthroughthis,beingthatIcreatedanequalmethodinclassPlanoDAOanddoesnotshowthisfault.

Andwhatisthenameofthisfunctionalityformetostudy,doesthishavetobewithlambda?

Thankyou

Answer:

JusttonoteIeditedmycodeastheconfreresreporteditanditlookedlikethis:

this.Model=newEmpresa{Id=Convert.ToInt32(data["id"]),
                Nome = Convert.ToString(data["nome"]),
                RazaoSocial = Convert.ToString(data["razao_social"]),
                Cnpj = Convert.ToString(data["cnpj"]),
                Status = Convert.ToString(data["status"]),
            };

            this.Model.Plano = new Plano
            {
                Id = Convert.ToInt32(data["plano_pk"]),
                Nome = Convert.ToString(data["plano_nome"]),
                Valor = Convert.ToDouble(data["plano_valor"])
            };

            this.Model.Endereco = new Endereco
            {
                Logradouro = Convert.ToString(data["logradouro"]),
                Numero = Convert.ToString(data["numero"]),
                Bairro = Convert.ToString(data["bairro"]),
                Complemento = Convert.ToString(data["complemento"]),
                Cep = Convert.ToString(data["cep"]),
                Cidade = Convert.ToString(data["cidade"]),
                Estado = Convert.ToString(data["estado"])
            };
    
asked by anonymous 11.05.2018 / 12:32

2 answers

5

This code should not be compiling:

public Empresa()
{
    Endereco = new Endereco();
    Plano = new Plano();
}

Note that you have declared the properties endereco and plano with the names completely in lowercase and as private , but here you are using those with the initial capitalized.

About lowercase

is case-sensitive , then Plano 1 is different from plano 2 .

1 - Refers to type Plano (the declared class)
2 - Refers to the property of type Plano whose name in that instance is plano

In the latest versions of Visual Studio, the highlighting presented in the IDE reinforces the compiler's interpretation of the written code.

Please note:

empresa = new Empresa 
{
    Endereco. // AQUI
}

The word Address is in green (or blue, or blue-green, or bluish green or whatever), just like Empresa or MySqlCommand , for example . This suggests that at that point you are referring to the Type, not the instance. Therefore, only static members of the Endereco class will be displayed if they exist.

About private

Even though you were using the property name correctly, you would not be able to assign the value of it, just as it is in , the private modifier limits the property access to the scope where it is declared (the class itself).

Solving

So, to fix it would be:

1 - Change the access modifiers of the fields in their classes to public , as in the example below:

public class Endereco
{
    public string logradouro;
    public string numero;
    public string bairro;
    public string complemento;
    public string cep;
    public string cidade;
    public string estado;
}

2 - Use property instead of the type constructors:

var endereco = new Endereco
    {
        logradouro = "algum nome",
        numero = "22A",
        // ... por aí vai
    };
  

(...) creating object without using the new operator (...)

I think this statement came from the confusion between types and type instances . So destroying dreams, to create the instance of type you will even need the new .

I hope I have helped.

    
11.05.2018 / 13:26
2

The correct one would be:

Endereco end = new Endereco()
{
    logradouro = "Rua teste",
    numero = "1200"
};

Without using the class name before the attribute (as you were doing).

However, there is a second problem, since all the properties of your classes are private.

When I add in VS Code, the following message appears:

  

'Endereco.logradouro' is inaccessible due to its protection level.

Changing to public would work and / or adding the assignment of values to some method, such as the constructor. The choice depends on the purpose of the class and its properties. If it is just a container, change to public . However, if it is a necessity for data to be populated at instance creation, use the constructor.

  

[...] but within it also has the class Plane and this yes, appear in the complete.

As it is not in the examples where it is being created, I can not state the differences. If it is being declared within the same scope of your class, no errors will occur.

    
11.05.2018 / 13:17