0

My question is not a problem indeed because i could solve it. I need someone who can explain why i was having that problem. In my original code i had

    int id1 = 1;
    int id2 = 2;
    FormAgregarProducto Agregar_Producto = new FormAgregarProducto();
    FormCaracteristicas Caracteristicas = new FormCaracteristicas();
    Deposito deposito1 = new Deposito(id1);
    Deposito deposito2 = new Deposito(id2);

Then when i was passing the arguments, it throwed "A field initializer cannot reference the nonstatic field, method, or property" so my solution was only putting static to the variables like this

    static int id1 = 1;
    static int id2 = 2;
    FormAgregarProducto Agregar_Producto = new FormAgregarProducto();
    FormCaracteristicas Caracteristicas = new FormCaracteristicas();
    Deposito deposito1 = new Deposito(id1);
    Deposito deposito2 = new Deposito(id2);

I barely understand my simple solution. Could someone explain to me why the error and what happen when i put static variables. Regards

allnuck
  • 77
  • 1
  • 6
  • 1
    Is your code inside a method or a function? – LarsTech Apr 18 '16 at 17:15
  • Read up on constructors - replace your dependent field initializers with constructor code and you'll be fine. – Luaan Apr 18 '16 at 17:18
  • 1
    Do yourself a favor and forget that field initializers exist until you have a couple of years of experience with programming. I have almost 10 years professional experience and I avoid using field initializers for anything but ints and strings. – Stilgar Apr 18 '16 at 17:20
  • 1
    Be aware that adding `static` changes the semantics of your program... – Lucas Trzesniewski Apr 18 '16 at 17:20
  • I was in the middle of answering when it was closed. A field initializer (ex: `Deposito deposito1 = new Deposito(id1);`) is called immediately when a constructor is called so your object hasn't been built yet. Referencing a field on an instance of the class (`id1`, aka `this.id1`) isn't legal because the instance hasn't been constructed yet. The static field is OK because that value is not attached the specific instance being created. – Frank Bryce Apr 18 '16 at 17:21
  • @JohnCarpenter That's not entirely true - the problem is that there's no guarantee that the initializers run in any order, so disallowing dependent initializers keeps the compiler from having to check for a dependency on order. – D Stanley Apr 18 '16 at 17:27
  • @DStanley The rule seems layed out [here](https://msdn.microsoft.com/en-us/library/aa645759(v=vs.71).aspx), and an order seems guaranteed. As you say, the order doesn't matter so it appears that they are "... executed in the textual order in which they appear in the class ...". Has it changed since this was written? – Frank Bryce Apr 18 '16 at 17:31
  • @JohnCarpenter Hmm - Version 5 still has the same spec, so I may be wrong. The marked duplicate used the same reason that I had in my mind but it seems to be wrong as well. – D Stanley Apr 18 '16 at 17:41
  • Well thanks for all the replies, i checked the question that was already and may be i understand a little more about this. I will put the code in the constructor. Thanks – allnuck Apr 19 '16 at 16:05

0 Answers0