Array<Value> access from other class


#1


I'm sorry to bother you good folks, but I'm at my wits end.


I have two classes handling serial and midi communications respectively. Then I have a class with sliders etc. I need to update the sliders from the two other classes.


So I made a fourth class, wherein I create a public Array<Value> faderValues, and refer the sliders to these. This seems works fine, but how do I update Vauea in that Array from the midi and serial classes?


I tried variations of:

ArrayClassName arrayClassName;
arrayClassName.faderValues[0].setValue (10);  // Does nothing and no errors

Probably some basic understanding I'm missing, so my apologies :-) Thankyou for your patience.

Søren


#2

Array’s operator[] returns a copy; it should work if you use

ArrayClassName arrayClassName;
arrayClassName.faderValues.getReference(0).setValue (10); 

Be aware that getReference() won’t check array bounds. I’m also not sure whether there is a better alternative for this than using an Array (which I think is rather thought for PODs and the like).


#3

Thankyou for your suggestion! Unfortunately, nothing changed. Using the introducer I've made a simple project, just using a single Value object and a slider. I still can't change the Value object from outside the class, in a way the slider sees. Here's the code (without Main.cpp, and only a few lines from MainComponent.cpp).

What am I doing wrong (besides lacking knowledge :-) )?

MainComponent.h :

#include "JuceHeader.h"
#include "ValueClass.h"
#include "SerialClass.h"

class MainComponent  : public Component,
                       public SliderListener
{
public:
    MainComponent ();
    ~MainComponent();

    void paint (Graphics& g);
    void resized();
    void sliderValueChanged (Slider* sliderThatWasMoved);

private:
    ValueClass valueClass;

    ScopedPointer<Slider> slider;
};

MainComponent.cpp

    slider->getValueObject().referTo (valueClass.sliderVal);

SerialClass.h

#include "JuceHeader.h"
#include "ValueClass.h"

class SerialClass    : Timer
{
public:
    SerialClass ();
    ~SerialClass();

private:
    void timerCallback ();

    ValueClass valueClass;
};

SerialClass.cpp

#include "SerialClass.h"

SerialClass::SerialClass ()
{
    startTimer (1000);
}

SerialClass::~SerialClass ()
{
    stopTimer();
}

void SerialClass::timerCallback ()
{
    valueClass.sliderVal.setValue (12);
}

ValueClass.h

#include "JuceHeader.h"

class ValueClass
{
public:
    ValueClass ();
    ~ValueClass();

    Value sliderVal;
};

ValueClass.cpp

#include "ValueClass.h"

ValueClass::ValueClass ()
{
    sliderVal = 10;
}

ValueClass::~ValueClass () {}

#4

It looks like you have two separate instances of ValueClass each with their own Value.  One in MainComponent (which you refer the slider to) and a separate one in SerialClass which has no relation to the one in MainComponent.  You'll need to make both Value's point to the same underlying data for it to have a chance to work.