How to assign a reference of StringArray to a TextButton?

Hi everyone.
I am a beginner in both JUCE and programming. I am writing a virtual Drum Pads in JUCE.
For playing a specific sample, I followed Tutorial: Build an audio player which the code is like

void DrumPadsAudioProcessorEditor::playSample ()
{
    juce::File file (sampleToPlay);
    auto* reader = audioProcessor.formatManager.createReaderFor (file);
    if (reader != nullptr)
    {
        auto newSource = std::make_unique<juce::AudioFormatReaderSource> (reader, true);
        audioProcessor.transportSource.setSource (newSource.get (), 0, nullptr, reader->sampleRate);
        audioProcessor.transportSource.start ();
        audioProcessor.readerSource.reset (newSource.release ());
    }
}

And I have 12 drumpads which are exactly the same so I used a juce::OwnedArray:

struct CustomDrumPad :juce::TextButton
{
    CustomDrumPad () :juce::TextButton ()
    {
        juce::TextButton::setColour (buttonColourId, juce::Colours::aquamarine);
    }
};
juce::OwnedArray<CustomDrumPad> drumPads;
void DrumPadsAudioProcessorEditor::createDrumPads (int newDrumPadRows, int newDrumPadColumns)
{
    //set the properties of the Drum Pads
    auto bounds = getLocalBounds ();
    auto distance = bounds.getWidth () * 0.9f / newDrumPadColumns;
    auto size = 0.9f * (bounds.getWidth () * 0.9f / newDrumPadColumns);
    auto margin = 0.05f * bounds.getWidth ();
    for (int i = 0; i < newDrumPadRows; i++)
    {
        for (int j = 0; j < newDrumPadColumns; j++)
        {
            auto* drumPad = new CustomDrumPad;
            drumPads.add (drumPad);
            addAndMakeVisible (drumPad);
            auto x = margin + 0.5f * distance + j * distance - 0.5f * size;
            auto y = margin + 0.5f * distance + i * distance - 0.5f * size;
            drumPad->setBounds (x, y, size, size);
        }
    }
    for (int i = 0; i < drumPads.size (); i++)
    {
        drumPads[i]->setButtonText (juce::String (i + 1));
    }
}

Now I hope that each Drum Pad has its own string for being read in playSample(), I tried this way but failed.

juce::StringArray samples
    {
        "C:\\Audio\\File.wav",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
        "...",
    };
void DrumPadsAudioProcessorEditor::chooseSample ()
{    
    for (int i = 0; i < 12; i++)
    {
        if (drumPads[i]->isDown ()) sampleToPlay = samples.getReference (i);
        else
        {
            sampleToPlay.clear ();
        }
    }      
}

Could anybody give me some advice? Thanks!

I didn’t read all your post but IMHO some commas are missing above.

Thanks nicolasdanet, there are commas in my code. The commas are just missing in this post, I will update it.

After this loop, sampleToPlay will only hold a value if the very last pad was down.

Could you please give me some advice reuk?

You could adjust your logic a bit so that you directly call playSample (samples.getReference (i)) if a particular drum pad is down.

Thanks reuk! This really helps me a lot.

Update:

Thanks to the advice from reuk, I changed the code into:

for (auto *drumPad : drumPads)
    {
        if (samples.size () == drumPads.size ())
            drumPad->onClick = [this, drumPad] { sampler (samples.getReference (drumPads.indexOf (drumPad))); };
    }

Then it works perfectly. I was just not very clear about the lambda expression.