Differences in constructors using property vs field (field)

Asked

Viewed 344 times

4

namespace WMB.CieloB
{
     internal class FuncoesCielo
     {
           internal FuncoesCielo(int iDC, Boleto boleto) 
           {
                IDCliente = iDC;
                this.boleto = boleto;
            }

            public int IDCliente { get; set; } 

            internal Transaction PagamentoComToken()
            {
                 var holder = cielo.holder(IDCliente);
                 Order orderDadosDoboleto = cielo.order(boleto.BoletoId.ToString(), boleto.Valor );
            }
      }
}

I’m calling this class through:

namespace WMB.CieloB
{
    public class Cobrancas
    {

        public bool ExecutarRecorrenciaCompleto()
        {
            IDC = 3;
            var boleto = db.boleto.Find(1);//apenas exemplo
            var NewPagamento = new FuncoesCielo(IDC, boleto);
            var transacao = NewPagamento.PagamentoComToken();
}
}}

That is both the property Idclient and boleto will be available within the class, but one is property (Idclient), another is a field (Boleto) I normally declare a field equal to a variable in this case nor declare.

What is the difference between using the IDCliente as a method of this.boleto that has not even been declared (Boleto boleto;) but I can access it within the class.

  • Well, you don’t have the property or the field boleto, I guess it was just a slip, right? MyVar also came out of nowhere. I’m not sure what you’re asking. Want to know why you have to create a property? You want to know if you could use a public field and not have the property? Want to know the difference between putting the value in the field by the constructor or by the property?

  • It was not a slip, even not creating yet I can access it! Only the difference between a property and a field that is not declared.

  • Perhaps it is the case to put the whole class to give more context. At least put as the field boleto is declared.

  • I think the question has now become clearer, if I put my specific class I believe that will only serve for me, I modified the question so that I can help someone else.

  • 1

    This is weird. I think without the context, you can’t see what’s wrong. It’s very strange not having a field boleto and strange working. In strange situations, without context it is difficult to know what is happening. I want to try to reproduce the problem here, but only with that I can not.

  • 3

    I answered what gave, but after the issues I’m thinking of voting to close as not clear. Each time makes less sense.

Show 1 more comment

1 answer

3


First it’s good to understand well what a builder is for. Then it starts clearing up because it’s different to boot it or the property. Unless you’re using C#6 or higher and the initialize directly with a default value. The same can be done with the field, in all versions you can always initialize with default value. Otherwise, it is possible to initialize the public properties/fields on the object initialization. It has a similar effect to the constructor.

If the data is not initialized during the construction of the object, by constructor or not, its initial value will be a pattern of the created type. It will be a 0, a null, etc. And then it can be changed if the property/field allows this. Question that helps to understand this.

The fact that there is a property means that it can be accessed externally. Whether it can change its value or not depends on how it is declared. Remembering that it is common for a property to have a field to store the value, the state, even if it does not appear in the code, this field is usually private.

It is possible to use a public field to achieve the same effect, but this usually hurts abstraction/encapsulation and there is no granular control over access. Both reading and writing are public. Another question can help understand the property a little more.

Of course, if you have a private field only the class itself can access it. A private field can be accessed in the constructor or any method of it, but nothing from outside.

As for the field boleto accessible I would say that is impossible, mainly by being this.boleto, but I didn’t see the full context.

AP didn’t want to put a bigger context, so now I’m going to start speculating on the fact of the field boleto be accessible even without being declared in the class.

If the class inherits from one another and the other has a field with visibility protected, then you are accessing the base property field.

If there is a field in another class in the same Assembly visible internal would also give access to the other classes.

The way it is the code nor compiles.

After the edits the code got even weirder.

  • I will put complete.. and compile yes.. rs

  • really had a field field, as the class was great did not see that the visual studio put down there private boleto boleto;

  • So it’s something that you have in your project. It’s hard to answer without seeing the whole context. Now I’m thinking of voting to close as not reproducible (I haven’t voted for anything clear yet, I’m giving you a chance to better define the problem). If you can provide any more information to help identify how you are.

  • I don’t know, send screenshot of VS, show what else is in this project. I really wanted to understand why you’re compiling there.

  • Did you solve it? What was it?

  • 2

    Yes, my total failure, As the class was great I did not see that the visual studio put down there private boleto boleto; I thought only with this. Boleto would be accessible, I thought it was some ultra super novelty of C# 6.. rs

Show 1 more comment

Browser other questions tagged

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