# Problems with width from class within class

**URL:** https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742
**Category:** Coding Questions
**Created:** [July 13, 2018, 2:40pm UTC](https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742 "2018-07-13T14:40:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 13, 2018, 2:40pm UTC](https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742/1 "2018-07-13T14:40:05Z")

</div>

Hello, I’m having problems with getting the correct value for screenwidth using width from a class within a class.  
I modified the example code that I found on this forum to illustrate the problem.

If i call width from the setup it gives the correct value of 1280, but when i call it from the Class room it always gives 100.  
Is this a known bug or am I getting something wrong?

```auto
House myHouse = new House();
int myblock; 
void setup() {
 
  size (1280, 720);
 //***THIS GIVES CORRECT VALUE FOR WIDTH (1280)***
  myblock = width;
  
  myHouse.rooms[0].name="";
  myHouse.rooms[0].wifi = true;
  myHouse.rooms[0].isDefined = true;
 
  myHouse.rooms[1].name="Bedroom";
  myHouse.rooms[1].wifi = false;
  myHouse.rooms[1].isDefined = true;
  myHouse.rooms[1].colour=color(5, 0, 255);   
 
  myHouse.rooms[2].name="Child 1";
  myHouse.rooms[2].wifi = false;
  myHouse.rooms[2].isDefined = true;
  myHouse.rooms[2].windows=5;
  myHouse.rooms[2].colour=color(5, 210, 255);   
 
  background(9);
}//function
 
void draw() {
  background(9);
  myHouse.display();
  
}//function
 
// ==========================================
 
class House {  
 
  // default values for a house
  color colour=color(0, 255, 0);  
  int size=310;  
  int houseNumber=1;  
  String street="Correct blocksize from setup";
  String name="House of family Stone";
 
  // array of rooms in the house
  Room[] rooms = new Room [22];
 
  // constructor
  House () {
    // pre init rooms 
    for (int i =0; i< rooms.length; i++) {
      rooms[i] = new Room();
    }
  }//constructor
 
  // method
  void display() {
    // display house
    fill(colour);
    text(myblock+"\n"+street+" "+houseNumber, 44, 44);
 
    float verticalDistanceRooms = 60; 
 
    int i=0;
    for (Room currentRoom : rooms) {
 
      if (currentRoom.isDefined) {
        currentRoom.display(244, i*verticalDistanceRooms+88 );
 
        stroke(255);
        // horizontal line
        line (66, i*verticalDistanceRooms+88, 
          210, i*verticalDistanceRooms+88); 
        // vertical line
        line (66, 0*verticalDistanceRooms+88-20, 
          66, i*verticalDistanceRooms+88); 
 
        i++;
      }
    }//for
  }//method
  //
}//class 
 
// =====================================================
 
class Room {  
 
  // default values for a room 
  color colour=color(255, 0, 0);   
  int windows=2;  
  String name="";
 
  boolean wifi=false;
  boolean isDefined=false;
  
  //***THIS GIVES WRONG VALUE FOR WIDTH (always 100)***
  int block = width;
  
  
  // no constructor
 
  // method 
  void display(float x, float y) {
    // display room
 
    // check wifi 
    String hasWifiText = "Wrong blocksize from Class room"; // default text
    if (wifi) {
      hasWifiText = "Wrong blocksize from Class room"; // text for room that has wifi
    } 
 
    // display room using color, name and wifi text etc.
    fill(colour);
    text(block
      +"\n"
      +hasWifiText
      , 
      x, y);
  }
  //
} //class
//

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 13, 2018, 3:39pm UTC](https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742/2 "2018-07-13T15:39:39Z")

</div>

The problem is caused because you are creating the Room objects before the sketch width is set in `setup`

Change the first line to

```auto
House myHouse;

```

then create the house in setup

```auto
void setup() {
 
  size (1280, 720); // this will set the width and height variable
  myHouse = new House(); // Now crerate the house once width is set correctly!
  //***THIS GIVES CORRECT VALUE FOR WIDTH (1280)***
  myblock = width;
  
  myHouse.rooms[0].name="";
  myHouse.rooms[0].wifi = true;
  myHouse.rooms[0].isDefined = true;
  ...

```

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 13, 2018, 4:01pm UTC](https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742/3 "2018-07-13T16:01:15Z")

</div>

Works. Thank you very much. That was causing me headache.

---

<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: [July 13, 2018, 5:03pm UTC](https://discourse.processing.org/t/problems-with-width-from-class-within-class/1742/4 "2018-07-13T17:03:15Z")

</div>

- [https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item\_3](https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item_3)
