# Nedd help with a class

**URL:** <https://discourse.processing.org/t/nedd-help-with-a-class/4409>\
**Category:** Coding Questions\
**Created:** [October 13, 2018, 12:34am UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409 "2018-10-13T00:34:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricSaumier](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EricSaumier](https://discourse.processing.org/u/EricSaumier)\
**Post date:** [October 13, 2018, 12:34am UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409/1 "2018-10-13T00:34:27Z")

</div>

Hello!

I’m getting back into coding after 20 some years.. I’ve been toying around with processing for a year now, doing simple things.. but this time, I want to learn classes

I have followed this tutorial [Objects](https://processing.org/tutorials/objects/) by _Daniel Shiffman_ on Processing site worked well. until now..  
I ran into a a problem that I just can’t figure out.

### What I want to do

Control 3 different VLC player on different computer. Works flawless with “regular” code.

I decided to create a class to “clean” the code instead of having 3 time the same code for each instance different VLC player.

I hope it make sense, thanks for taking the time to help out..

- Eric

### error at compile

> The constructor Client (VLC\_Class.VLC, String, int) is undefined

### my code

```auto
import processing.net.*;

VLC vlc1;

public void setup() {
  size(600, 600, JAVA2D);

  noStroke();
  //client = new Client(this, "127.0.0.1", 4212);
  //client.write("1234"+"\r\n");

  vlc1 = new VLC("127.0.0.1", 4212, "1234");
}

void StartVLC() {

    vlc1.play();
}

void draw() {

 // client.write("play"+"\r\n");
  
  
  StartVLC();
  
  exit();
}

```

### my class

```auto
class VLC {

  String serverIP;
  int portNumber;
  String password;
  String enter = "\r\n";
  Client client;

  VLC( String tempIP, int tempPort, String tempPass) {

    serverIP = tempIP;
    portNumber = tempPort;
    password = tempPass;
  }

  void connect() {
    println("connect");
    client = new Client(this, serverIP, portNumber);
    client.write(password+enter);
  }

  void play() {
    println("play");
    client.write("play" +enter);
  }

  void next() {
     client.write("next" +enter);
  }

  void pause() {
     client.write("pause" +enter);
  }
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 13, 2018, 10:15pm UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409/2 "2018-10-13T22:15:20Z")

</div>

The problem is that when you were setting up the client inside `setup()`, the keyword `this` used in the constructor refered to the PApplet that `setup()` belongs to. Once you moved this constructor call for `Client()` inside your class, the `this` keyword now refers to that class, and not the PApplet that it was referencing before.

The solution is to pass the reference to your PApplet into your own constructor:

```auto
// inside setup():
  vlc1 = new VLC(this, "127.0.0.1", 4212, "1234");

```

```auto
// Fix your class to deal with the new parameter:
  Client client;
  PApplet parent_app;

  VLC( PApplet ipa, String tempIP, int tempPort, String tempPass) {
    parent_app = ipa;
    serverIP = tempIP;
    portNumber = tempPort;
    password = tempPass;
  }

  void connect() {
    println("connect");
    client = new Client(parent_app, serverIP, portNumber); // The this that was here was the problem - now changed.
    client.write(password+enter);
  }

```

You could also have added a global variable to store this reference to the parent PApplet in setup and use it inside your class, should you not desire to pass it as a parameter.

Untested code - YMMV

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 13, 2018, 11:08pm UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409/3 "2018-10-13T23:08:56Z")

</div>

- [Forum.Processing.org/two/discussion/23440/referencing-to-mainapp-papplet-instead-of-this#Item\_3](http://Forum.Processing.org/two/discussion/23440/referencing-to-mainapp-papplet-instead-of-this#Item_3)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 13, 2018, 11:09pm UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409/4 "2018-10-13T23:09:18Z")

</div>

> [@TfGuy44](#):
>
> You could also have added a global variable to store this reference to the parent PApplet in setup and use it inside your class, should you not desire to pass it as a parameter.

@EricSaumier – Using a global variable certainly an option, but the first pattern that @TfGuy44 recommends – passing in a PApplet with `this` – is very standard in Processing. Many of the major libraries follow this pattern, and require you to initialize a major library object with `this` in order to work their magic (see ControlP5, G4P, PeasyCam, etc etc). For example, you fire up ControlP5 with:

```auto
cp5 = new ControlP5(this);

```

---

<div class="post-metadata">

**Author:** ![EricSaumier](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EricSaumier](https://discourse.processing.org/u/EricSaumier)\
**Post date:** [October 15, 2018, 1:23am UTC](https://discourse.processing.org/t/nedd-help-with-a-class/4409/5 "2018-10-15T01:23:55Z")

</div>

I took the time to digest everything… I’m starting to understand things a bit more, and it starts make sense. I need to really undestand PApplet… with GoToLoop link, I think i’m on the right path…

Thanks alot for the help and the time you guys invest helping others. One day, I’ll give back!

@TfGuy44 … I tested your "untested’ solution… worked on first try.  
@jeremydouglass That will be the way I’ll go… seems to make more sense
