# Newb - if I declare a variable in PluginEditor.h , whats the right way to access it in PluginProcessor.cpp?

**URL:** <https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301>\
**Category:** General JUCE discussion\
**Created:** [February 6, 2021, 9:22pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301 "2021-02-06T21:22:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian-SAfc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ian-safc/32/17877_2.png) [@Ian-SAfc](https://forum.juce.com/u/Ian-SAfc)\
**Post date:** [February 6, 2021, 9:22pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/1 "2021-02-06T21:22:55Z")

</div>

I have this which I declared in PluginEditor.h, `mIansTextEditor`  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/2/2aee7256c604685341065f2784aa46fff976bf18.png)

Its a textbox. In PluginEditor.cpp I draw it on the screen, set its size etc.  
But in PluginProcessor.cpp , prepareToPlay, I want to write some text in there. But PluginProcessor.cpp does not have access to mIansTextEditor.  
Whats the best way to make it available in PluginProcessor.cpp ?

---

<div class="post-metadata">

**Author:** ![stfufane](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@stfufane](https://forum.juce.com/u/stfufane)\
**Post date:** [February 6, 2021, 10:30pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/2 "2021-02-06T22:30:48Z")

</div>

You must take the problem the opposite way. The processor should never try to access anything that belongs to the editor, for the simple reason that the editor might not be there at all. When you close your plugin window, the editor instance is destroyed.

But you can create a member in your processor that your editor can access. In prepareToPlay you set this value in the processor, then in the editor paint method you can read the content of this variable and set the text box content. Your editor has a reference to the processor so you can easily do that 🙂

If you often need to update this value, you can add a Timer in your editor to check the processor value regularly and update the text box

---

<div class="post-metadata">

**Author:** ![Ian-SAfc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ian-safc/32/17877_2.png) [@Ian-SAfc](https://forum.juce.com/u/Ian-SAfc)\
**Post date:** [February 6, 2021, 11:33pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/3 "2021-02-06T23:33:05Z")

</div>

> [@stfufane](#):
>
> But you can create a member in your processor that your editor can access.

Thanks, thats better. However, only one error remains in PluginEditor.cpp:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/b/bb45b87f58bdac0443bac998b73617c09892aea2.png)  
The resized() function does not take in the &p that references the processor.  
This function in PluginEditor.cpp does so it works there.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/6/6f0a03d14a3ac16abe3439a174814dab19af3af7.png)  
resized() does not. Any suggestions? Should I make resized() take in &p?

---

<div class="post-metadata">

**Author:** ![holy-city](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@holy-city](https://forum.juce.com/u/holy-city)\
**Post date:** [February 7, 2021, 12:09am UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/4 "2021-02-07T00:09:37Z")

</div>

It is extremely suspicious that your processor owns a gui component like a “text editor.”

the member name in the editor is `audioProcessor`. `p` is the parameter passed in the constructor as an argument.

---

<div class="post-metadata">

**Author:** ![Ian-SAfc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ian-safc/32/17877_2.png) [@Ian-SAfc](https://forum.juce.com/u/Ian-SAfc)\
**Post date:** [February 7, 2021, 12:20am UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/5 "2021-02-07T00:20:09Z")

</div>

> [@holy-city](#):
>
> the member name in the editor is `audioProcessor` .

Ok, thanks, this works now:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/8/8ea8f900e15d9529157f5217b7cb6cf2f805c0b7.png)  
I am inserting a textbox just to get to know that class and other widgets.

---

<div class="post-metadata">

**Author:** ![stfufane](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@stfufane](https://forum.juce.com/u/stfufane)\
**Post date:** [February 7, 2021, 9:02am UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/6 "2021-02-07T09:02:30Z")

</div>

No you should not do it like that. The mIansTextEditor component should belong to the editor, not the processor. The processor should just have a member containing the actual text. Then the editor will fetch the text from the processor and use it to set the mIansTextEditor content.

---

<div class="post-metadata">

**Author:** ![Ian-SAfc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ian-safc/32/17877_2.png) [@Ian-SAfc](https://forum.juce.com/u/Ian-SAfc)\
**Post date:** [February 7, 2021, 3:41pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/7 "2021-02-07T15:41:35Z")

</div>

> [@stfufane](#):
>
> The processor should just have a member containing the actual text.

thanks, but what if I am writing to the text box in the `prepareToPlay()` function which is in the PluginProcessor.cpp? I need to have access to the component at that point.

---

<div class="post-metadata">

**Author:** ![stfufane](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@stfufane](https://forum.juce.com/u/stfufane)\
**Post date:** [February 7, 2021, 4:28pm UTC](https://forum.juce.com/t/newb-if-i-declare-a-variable-in-plugineditor-h-whats-the-right-way-to-access-it-in-pluginprocessor-cpp/44301/8 "2021-02-07T16:28:23Z")

</div>

In your processor

```cpp
public:
   juce::String& getMyText() { return myText; }
  void prepareToPlay() { myText = "myValue"; }
private:
  juce::String myText;

```

In your editor

```cpp
void paint(juce::Graphics& g) { mIansTextEditor.setText(audioProcessor.getMyText());

```

Your processor does not need to know what the editor is doing. If your plugin window is closed when prepareToPlay is called, there will be no editor instance at all. It’s the editor’s job to fetch the information from the processor. If the processor must often change this information you can use a `juce::Timer` in your editor to fetch it and repaint. But there’s no point wanting to update the textbox exactly when prepareToPlay is called, processor and editor run in different threads.
