# Particle.variable declaration in object constructor stopped working

**URL:** <https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695>\
**Category:** Troubleshooting\
**Tags:** photon\
**Created:** [May 12, 2019, 5:47pm UTC](https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695 "2019-05-12T17:47:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzouck](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@jzouck](https://community.particle.io/u/jzouck)\
**Post date:** [May 12, 2019, 5:47pm UTC](https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695/1 "2019-05-12T17:47:19Z")

</div>

I declared a variable using Particle.variable() in the constructor for a c++ object, using a private int variable in the class. It worked great for two years, but when I went to recomple and flash it the other day it compiled fine, but the photon would stop with a flashing red SOS. I tracked it down to the Particle.variable call in the object constructor by commenting that statement out, and seeing it ran fine (but without actually creating the variable of course.)

Has there been a change in the runtime to disallow declaring object private members as Particle.variables?

---

<div class="post-metadata">

**Author:** ![jzouck](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@jzouck](https://community.particle.io/u/jzouck)\
**Post date:** [May 12, 2019, 7:11pm UTC](https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695/2 "2019-05-12T19:11:24Z")

</div>

Here’s a simple example:

obclass.h:

```auto
// Test object class
//
#ifndef OBCLASS_H
#define OBCLASS_H

#include "application.h"
class obclass {
    private:
        int state; // 1 = on, 0 = off
    public:
        obclass( char *vname_state);
};
#endif

```

obclass.cpp:

```auto
#include "obclass.h"

obclass::obclass( char *vname_state ){
        // Commenting this out makes the program not fail!
        Particle.variable(vname_state, state);
}

```

vartest.ino:

```auto
#include "obclass.h"

static obclass ob = obclass( "obname");
void
setup ()
{
        Particle.publish ("startup", "Started obclass test program v2.4");
}

void
loop()
{
        Particle.publish("status", "loop interation");
        delay (5000);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 12, 2019, 8:05pm UTC](https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695/3 "2019-05-12T20:05:59Z")

</div>

Yes, there was a change.  
At the time of instatiation of your object the `Particle` object is not fully instantiated which then leads to an SOS panic.  
The way to get around this - and how it should have been done always - is to add a `class::begin()` or `class::init()` function which is executed by _your_ code and not implicitly during object construction.

BTW, I don’t think that exposing a `private` field of a class this way isn’t really best style.

---

<div class="post-metadata">

**Author:** ![jzouck](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@jzouck](https://community.particle.io/u/jzouck)\
**Post date:** [May 13, 2019, 3:53pm UTC](https://community.particle.io/t/particle-variable-declaration-in-object-constructor-stopped-working/49695/4 "2019-05-13T15:53:43Z")

</div>

Thanks for the explanation.

I’ll try your suggestion, but it makes for somewhat less clean structure,. I wanted to hide as much detail in my objects as possible including declaration of particle.variables, and doing this in the constructor seemed a simple way to accomplish this.

But I now see an alternative implementation not involving the extra setup() function that should be almost as clean.
