# Dynamically assign a variable or object

**URL:** <https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543>\
**Category:** Development\
**Created:** [July 26, 2019, 10:44am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543 "2019-07-26T10:44:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [July 26, 2019, 10:44am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/1 "2019-07-26T10:44:14Z")

</div>

Hi everyone,

I am brandnew to Juce and C++, but my first plugin is coming along with the help of all the topics here on the forum and things on Google.

What I have not figured out so far is how to do the following:

I have three faders: fader0, fader1, fader2

In my code I do a lot of formatting to the labels belonging to the faders etc, but I just down know how to address these faders in a loop.

So if I were to do:

i = 3;  
fader+i.setValue = 12345;

in a loop that would be super helpful.

What do I need to look for online or can somebody help me please! 😉

Many thanks!

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 26, 2019, 11:09am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/2 "2019-07-26T11:09:04Z")

</div>

Put your components into an array or vector. There’s however a twist that you have to then use pointers, with all the potential problems those can bring. Juce has a handy class OwnedArray that helps somewhat with that, though.

Something like :

```auto
// as a member variable of your editor class
OwnedArray<Slider> mySliders;

// in the constructor of your editor class
for (int i=0;i<numberOfSlidersWanted;++i)
{
  Slider* slider = new Slider;
  addAndMakeVisible(slider);
  // whatever else initialization you need for the slider...
  mySliders.add(slider);
}

// then to access the sliders elsewhere in the code with a loop
for (int i=0;i<mySliders.size();++i)
{
  mySliders[i]->setValue(foo);
}

```

---

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [July 26, 2019, 11:21am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/3 "2019-07-26T11:21:08Z")

</div>

Wow!

thanks for the speedy reply! Will to try to use that right away.

And If I were to generate labels I just use:

OwnedArray myLabels;

// in the constructor of your editor class  
for (int i=0;i\<numberOfLabelsWanted;++i)  
{  
Label\* label = new Label;  
addAndMakeVisible(label);  
// whatever else initialization you need for the slider…  
myLabels.add(label);  
}

?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 26, 2019, 11:21am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/4 "2019-07-26T11:21:50Z")

</div>

`OwnedArray<Label>` if you want to use Labels.

---

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [July 26, 2019, 11:28am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/5 "2019-07-26T11:28:29Z")

</div>

> [@Xenakios](#):
>
> OwnedArray

cool!

---

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [July 27, 2019, 8:05am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/6 "2019-07-27T08:05:17Z")

</div>

So I’m almost there I think. But I’m not seeing the sliders only after I resize. Is there a way I can trigger this so I can put all the bounds information is resize?

this is in my MainComponent.cpp:

# /\*

```
This file was auto-generated!

```

==============================================================================  
\*/

#include “MainComponent.h”

//==============================================================================  
MainComponent::MainComponent()  
{  
setSize (600, 400);

```
// in the constructor of your editor class
for (int i=0;i<3;++i)
{
    Slider* slider = new Slider;
    addAndMakeVisible(slider);
    // whatever else initialization you need for the slider...
    mySliders.add(slider);
}

```

}

MainComponent::~MainComponent()  
{  
}

//==============================================================================  
void MainComponent::paint (Graphics& g)  
{

}

void MainComponent::resized()  
{

```
for (int i=0;i<mySliders.size();++i)
{
    mySliders[i]->setBounds(100, i*100, 100, 20);
}

```

}

and this in my MainComponent.h file:

#pragma once

#include “…/JuceLibraryCode/JuceHeader.h”

_//==============================================================================_

_/_\*

_This component lives inside our window, and this is where you should put all_

_your controls and content._

\*_/_

**class** MainComponent : **public** Component

{

**public** :

_//==============================================================================_

MainComponent();

~MainComponent();

_//==============================================================================_

**void** paint (Graphics&) **override** ;

**void** resized() **override** ;

**private** :

_//==============================================================================_

_// Your private member variables go here…_

_// as a member variable of your editor class_

OwnedArray mySliders;

JUCE\_DECLARE\_NON\_COPYABLE\_WITH\_LEAK\_DETECTOR (MainComponent)

};

Am I missing something?

The sliders will only show up after I resize…

---

<div class="post-metadata">

**Author:** ![tomto66](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tomto66/32/4916_2.png) [@tomto66](https://forum.juce.com/u/tomto66)\
**Post date:** [July 27, 2019, 11:10am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/8 "2019-07-27T11:10:29Z")

</div>

Do the setBounds in paint() - that’ll do the trick! - **EDIT: Bad idea, shame on me!**

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 27, 2019, 11:18am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/9 "2019-07-27T11:18:42Z")

</div>

No, doing setBounds in paint() is not really a good idea…the resized() method is the more appropriate place to do it.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 27, 2019, 11:21am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/10 "2019-07-27T11:21:57Z")

</div>

Do your editor’s setSize call at the end of its constructor after you’ve created and added the child components, not at the beginning.

---

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [July 27, 2019, 11:27am UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/11 "2019-07-27T11:27:00Z")

</div>

Thanks!

Your answers mean a lot to me 😉

Have a great weekend!

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [July 27, 2019, 4:50pm UTC](https://forum.juce.com/t/dynamically-assign-a-variable-or-object/34543/12 "2019-07-27T16:50:07Z")

</div>

> [@tomto66](#):
>
> Do the setBounds in paint() - that’ll do the trick!

flagged for moderation lol
