3
Visual Studio 2017 suggested something that I was surprised, the creation of object without using the new operator, I was surprised, because I’m coming from Java and it’s been a year since I stopped and decided to go back to study.
Well, when creating a new Company object, it does not appear the fields of the Address class that are declared within the Company class, but within it also has the Plan class and this yes, appear in the complete.
I cannot see wrong, because the two classes are almost equal, apart from the fact that Plano receives inheritance from the Persistant class.
Below follows the code to see from 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
}
}
Flat Class
namespace Cobranca.pkgModel
{
public class Plano : Persistant
{
private string nome;
private string tipoCobranca;
private double valor;
//métodos getter e setter
}
}
Address Class
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 follows the print I took from the screen
Can anyone answer me why I’m going through this, since I created an equal method in class PlanoDAO
and does not present this flaw.
And what name of this feature for me to study, that has to do with lambda
?
Thank you
Answer:
Just for the record I edited my code as the confreres informed and it was like this:
this.Model = new Empresa
{
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"])
};
I could be wrong, but I think the OP did not add in the question the
getter
andsetter
, see that you have a comment //getter and Setter methods, soon theEndereco = new Endereco();
compile– Barbetta
@Barbetta, in the class
Empresa
the property of typeEndereco
has the nameendereco
. Nowhere is a variable, field or property being declared with the nameEndereco
(With the initial capital). I don’t understand how getter / Setter would be related to this. Could you please clarify?– Diego Rafael Souza
@Barbetta, I think I understand what you mean. Maybe he has the field and property declared in the class, where the property would have the same type name. If that’s the case, you’re right, compile. And my answer would be all based on the absence of that information.
– Diego Rafael Souza
I think he declared the
getters
andsetters
"the old fashioned", declaring a private variable, and below thegetters
andsetters
, Anyway, in my view your answer answers the question very well. + 1– Barbetta
@Barbetta on the first statement, yes, I did not add to not fill the question too much
– Macario1983
@Diegorafaelsouza on the second post, I thought inside the body building the object
Empresa
, it would already automatically create the call toEndereco
, I even thought about doing the two separately yes– Macario1983
@Barbetta on declaring getter and Setter the old-fashioned, what is the current fashion? Because I use the
VS
to help me in creating– Macario1983
@Diegorafaelsouza and @Barbetta, I will check here because I did it the old and less practical way, ie for example
Empresa.Endereco.Logradouro = "valor";
– Macario1983
@Macario1983 if the property has nothing else on
get
andset
, the "current brand" would bepublic Endereco Endereco { get; set; }
. Just this property without the private camp. But I based my answer on a possible confusion of understanding on your part (and possibly on mine, because part of the code was omitted - I can’t remember when I used the termgetter
orsetter
for the last time, it’s justpropriedade
orcampo
)– Diego Rafael Souza
@Macario1983 It may be interesting you evaluate if it is not worth to pass as parameter of constructor
Empresa
an objectEndereco
filled out.– Diego Rafael Souza
@Diegorafaelsouza I already instancio in the builder the objects
Plano
andÈndereco
. But I can assess yes, only a doubt, this feature has some specific name?– Macario1983
@Not Macario1983. We call it
propriedade autodeclarada
(the one that doesn’t need a fieldprivate
)– Diego Rafael Souza
VS has a shortcut for property creation, in its class type
prop
and tightenTAB
twice. If you writepropfull
andTAB
twice, he creates her the "old-fashioned"– Barbetta
Gee, that’s nice @Barbetta, but they didn’t answer me, this way of creating object without using relatives on
new
, as it is called, it was VS himself who suggested me to reduce code.– Macario1983
@Macario1983, the constructor without parentheses is known as standard builder and alleviate the properties of the type at the time of instantiation we we call it initialization of the kind, This is one of several C facilitators#.
– Diego Rafael Souza
O C# 6 (
Visual Studio 2015
) introduced some new interesting facilitators that I think are worth getting acquainted with.– Diego Rafael Souza
@Diegorafaelsouza, it will be of great help these references, very dynamic the language.
– Macario1983