# Basic OwnedArray Questions

**URL:** <https://forum.juce.com/t/basic-ownedarray-questions/24950>\
**Category:** General JUCE discussion\
**Created:** [November 2, 2017, 10:16am UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950 "2017-11-02T10:16:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chamabreu](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@chamabreu](https://forum.juce.com/u/chamabreu)\
**Post date:** [November 2, 2017, 10:16am UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/1 "2017-11-02T10:16:35Z")

</div>

Hey,

i have 20 Labels (called for testing label1, label2… label20).  
And i want to access these Labels by numbers.  
As idea in a For-Loop, to write for every Label the name “MYLABEL”, instead of calling each label one by one in a line of code and set the name.

My idea is to do it in an OwnedArray of Labels. Could that work?  
As i understood OwnedArrays, i can make an Array of Pointers, each Pointer points to a Specific label.

For example could i:  
myOwnedArray[5].setName(“MYLABEL”);

and that would change the name of Label 6 to the given String?

That would be great.

Greetings,  
Manuel

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [November 2, 2017, 10:22am UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/2 "2017-11-02T10:22:59Z")

</div>

```
OwnedArray<Label> labels;

for (auto i = 0; i < 20; ++i)
   labels.add(new Label({}, "Label Number " + String(i)));

for (auto * label: labels)
  addAndMakeVisible(label);

```

You can do things like this …

```
label[num]->setText("new label text", dontSendNotification);

```

Note that as OwnedArray is an array of _pointers_ you need to use the -\> operator not the . operator to access the methods.

Bear in mind the label constructor takes the label text as the **second** parameter:

```
Label (const String &componentName=String(), const String &labelText=String())
```

---

<div class="post-metadata">

**Author:** ![chamabreu](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@chamabreu](https://forum.juce.com/u/chamabreu)\
**Post date:** [November 2, 2017, 10:35am UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/3 "2017-11-02T10:35:32Z")

</div>

Awesome!!!  
Just needed such an example to understand the working of the OwnedArray. Documentation has no real Examples, and i am very new to c++ and Juce, so examples help me alot!!

thought i need to create first 20 Labels.  
And then add pointers to each of them from the array.

Thanks, gonna get back here if i stuck on further operations!!

\<3

Manuel

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 2, 2017, 2:16pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/4 "2017-11-02T14:16:39Z")

</div>

> [@jimc](#):
>
> ```
> OwnedArray<Label> labels;
> 
> for (auto i = 0; i < 20; ++i)
> labels.add(new Label({}, "Label Number " + String(i)));
> 
> for (auto * label: labels)
> addAndMakeVisible(label);
> 
> ```

Since OwnedArray::add() returns the added pointer, you can even reduce to one loop:

```
OwnedArray<Label> labels;

for (auto i = 0; i < 20; ++i)
    addAndMakeVisible (labels.add (new Label({}, "Label Number " + String(i))));

```

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [November 2, 2017, 2:19pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/5 "2017-11-02T14:19:33Z")

</div>

Well I never notice that it did that.

Code here often looks like

```
for (auto i = 0; i < 20; ++i)
{
   auto newLabel = new Label({}, "Label Number " + String(i));
   addAndMakeVisible(newLabel);
   labels.add(newLabel);
}
```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 2, 2017, 2:23pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/6 "2017-11-02T14:23:22Z")

</div>

It just saves one variable. But your version is surely easier to understand. And I am pretty sure, the produced binary will be identical.  
Plus your first example shows nicely the new way how to iterate a container…  
Just posted it as a gimmick…

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [November 2, 2017, 2:24pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/7 "2017-11-02T14:24:01Z")

</div>

I heard that the code goes faster if the source file has fewer characters.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 2, 2017, 2:29pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/8 "2017-11-02T14:29:07Z")

</div>

Only if you use counterclockwise twisted AC cables, that take account for the earth rotation in the northern hemisphere…

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [November 2, 2017, 2:53pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/9 "2017-11-02T14:53:42Z")

</div>

That’s why we should use tabs instead :troll:

---

<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:** [November 2, 2017, 2:56pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/10 "2017-11-02T14:56:39Z")

</div>

You shouldn’t even jokingly say anything like that when newbies are around.

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [November 2, 2017, 3:15pm UTC](https://forum.juce.com/t/basic-ownedarray-questions/24950/11 "2017-11-02T15:15:28Z")

</div>

Newbies don’t go away, they will always be around. Lighten up. 🙂
