user14904: Lets say I have a sketch called mysketch.ino with these contents:
String globalString = "ThisisGlobal";
void setup(){}
void loop(){}

How can I pass THIS sketch mysketch.ino as an object or its reference to a .h file named
StringChanger.h with the following contents, so that StringChanger.h can change somevariables belonging to mysketch.ino.
class StringChanger{
   template <typename T> // or type of mySketch.ino i don't know what i'm doing 
   void changeString(T t) {  //a Reference Or Object Or Whatever
      t.globalString = "GotYouAndChangedYou";
   }
}

I wan't to have simple usage in the mySketch.ino.
For instance:
#include "StringChanger.h"

StringChanger change(**this**);  // perhaps default empty constructor here

String globalString = "ThisisGlobal";

void setup(){
    change.changeString(); // perhaps pass reference to **this** here
}
void loop(){}

How can I accomplish this (give a class an object so that it can change some value of that object) in general and not neccesarily in this context.
Errors I currently get:
invalid use of 'this' in non-member function
invalid use of 'this' at top level.


user4143: A sketch isn't an object. It is merely a collection of variables, functions and, of course, classes.
Your example seems to be trying to make things incredibly complex when they don't need to be.
There are a number of ways of referencing things in one file from another - whether or not they are in a class.  The simplest is to use the extern keyword.  This is used in a file where you want to reference a variable or function that is defined in the global scope of another file. It says "This variable is global, but it's defined somewhere else."
// Sketch
String myString = "foo";

// Library
extern String myString;
// ... later on ...
myString = "bar";

Of course, that means that the name of the variable is fixed and cannot change.  So you can pass the variable to whatever it is you want to use that variable in.  That could be as simple as passing it to your "changer" function:
// Sketch
String myString = "foo";
StringChanger change;
// ... later ...
change.changeString(myString);

// Library
class StringChanger {
    public:
        void changeString(String &stringToChange) {
            stringToChange = "bar";
        }
};

If you want to "tie" the variable to the class so you don't have to pass it each time you could pass it to the constructor and have the class remember it:
// Sketch
String myString = "foo";
StringChanger change(myString);
// ... later ...
change.changeString();

// Library
class StringChanger {
    private:
        String *_string;
    public:
        StringChanger(String &string) {
            _string = &string;
        }
        void changeString() {
            *_string = "bar";
        }
}

And there's a myriad of other ways of doing it.

14904: <p>Lets say I have a sketch called <strong>mysketch.ino</strong> with these contents:</p>

<pre><code>String globalString = "ThisisGlobal";
void setup(){}
void loop(){}
</code></pre>

<p>How can I pass <strong>THIS</strong> sketch <code>mysketch.ino</code> as an object or its reference to a <code>.h</code> file named
<code>StringChanger.h</code> with the following contents, so that <code>StringChanger.h</code> can change somevariables belonging to <code>mysketch.ino</code>.</p>

<pre><code>class StringChanger{
   template &lt;typename T&gt; // or type of mySketch.ino i don't know what i'm doing 
   void changeString(T t) {  //a Reference Or Object Or Whatever
      t.globalString = "GotYouAndChangedYou";
   }
}
</code></pre>

<p>I wan't to have simple usage in the <code>mySketch.ino</code>.
For instance:</p>

<pre><code>#include "StringChanger.h"

StringChanger change(**this**);  // perhaps default empty constructor here

String globalString = "ThisisGlobal";

void setup(){
    change.changeString(); // perhaps pass reference to **this** here
}
void loop(){}
</code></pre>

<p>How can I accomplish this (give a class an object so that it can change some value of that object) in general and not neccesarily in this context.
Errors I currently get:</p>

<pre><code>invalid use of 'this' in non-member function
invalid use of 'this' at top level.
</code></pre>
 4143: <p>A sketch isn't an object. It is merely a collection of variables, functions and, of course, classes.</p>

<p>Your example seems to be trying to make things incredibly complex when they don't need to be.</p>

<p>There are a number of ways of referencing things in one file from another - whether or not they are in a class.  The simplest is to use the <code>extern</code> keyword.  This is used in a file where you want to reference a variable or function that is defined in the global scope of another file. It says "This variable is global, but it's defined somewhere else."</p>

<pre><code>// Sketch
String myString = "foo";

// Library
extern String myString;
// ... later on ...
myString = "bar";
</code></pre>

<p>Of course, that means that the name of the variable is fixed and cannot change.  So you can pass the variable to whatever it is you want to use that variable in.  That could be as simple as passing it to your "changer" function:</p>

<pre><code>// Sketch
String myString = "foo";
StringChanger change;
// ... later ...
change.changeString(myString);

// Library
class StringChanger {
    public:
        void changeString(String &amp;stringToChange) {
            stringToChange = "bar";
        }
};
</code></pre>

<p>If you want to "tie" the variable to the class so you don't have to pass it each time you could pass it to the constructor and have the class remember it:</p>

<pre><code>// Sketch
String myString = "foo";
StringChanger change(myString);
// ... later ...
change.changeString();

// Library
class StringChanger {
    private:
        String *_string;
    public:
        StringChanger(String &amp;string) {
            _string = &amp;string;
        }
        void changeString() {
            *_string = "bar";
        }
}
</code></pre>

<p>And there's a myriad of other ways of doing it.</p>
