-1

The following code should return 2 values which are fields in the properties WallArea and GallonsOfPaint. This should be returned to the main method. The Room class contain 2 private methods CalcWallArea and CalcAmountOfPaint which will set the value for the two properties I just mentioned. The problem is my it will only return one of the other. I cannot get it to return both. The outcome should be when a user enter the length. width and height the methods will tell the square footage of the room and also tell how many gallons of paint it will take to paint the room. Currently when ran it will only tell the square footage or if I call the 2nd method then it will tell how many gallons of paint but it will not do both. Can someone please assist?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Array;

namespace PaintingRoomDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Room aRoom = new Room();
            string numberString;

            WriteLine("Please enter the length of the wall in feet");
            numberString = ReadLine();

            aRoom.Length = Convert.ToInt32(numberString);

            WriteLine("Please enter the width of the wall in feet");
            numberString = ReadLine();

            aRoom.Width = Convert.ToInt32(numberString);

            WriteLine("Please enter the height of the wall in feet");
            numberString = ReadLine();

            aRoom.Height = Convert.ToInt32(numberString);

            Write("The room area is: {0} and requires {1} gallons of paint",
                aRoom.WallArea, aRoom.GallonsOfPaint);
            ReadLine();
        }
    }
    class Room
    {
        private int wallArea; //These are data fields//
        private int numberOfGallonsOfPaintNeeded; //These are data fields//
        private int length; //These are data fields//
        private int width; //These are data fields//
        private int height; //These are data fields//
        private int total;
        public int Length //This is a property which provides access to the data field length
        {
            get {return length;}
            set {length = value;}
        }
        public int Width //This is a property which provides access to the data field width
        {
            get {return width;}
            set {width = value;}
        }
        public int Height //This is a property which provides access to the data field height
        {
            get {return height;}
            set { height = value; CalcWallArea();}
        }
        public int WallArea //This is a property that should return wallArea
        {
            get {return wallArea;}
        }
        public int GallonsOfPaint //This is a property that should return wallArea
        {
            get {return numberOfGallonsOfPaintNeeded;}
        }
        private void CalcWallArea() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            wallArea = (Length + Width + Length + Width) * Height;
            CalcAmountOfPaint();
        }
        private void CalcAmountOfPaint() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            if (wallArea <= 350)
                numberOfGallonsOfPaintNeeded = 1;

            int x = 1;
            if (wallArea >= 350)
                while (wallArea > 0)
                {
                    x++;
                    wallArea = wallArea - wallArea;
                    numberOfGallonsOfPaintNeeded = x;
                }
        }
    }
}
Demond
  • 109
  • 5
  • 1
    Welcome to SO Demond. For tips on attracting good answers, please see [ask]. – John Wu Oct 04 '18 at 23:26
  • The = operator doesn't create a permanent association between a property and a variable. When for example `aRoom.Length = num1` executes, it sets Length to 0 because `num1` is 0. Later, when you set `num1`, `aRoom.Length` isn't automatically updated. – Michael Liu Oct 04 '18 at 23:31
  • In other words, you want `aRoom.Length = Convert.ToInt32(numberString);` Do favor using Int32.TryParse to convert your information to an integer. – LarsTech Oct 04 '18 at 23:35
  • Thank you both so much. i am very new at coding. – Demond Oct 04 '18 at 23:36
  • Careful, `=` can kind-of create an association between references. Consult [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/). – Dour High Arch Oct 04 '18 at 23:47

2 Answers2

0

You change the value of variables

num1 = Convert.ToInt32(numberString);
...
num2 = Convert.ToInt32(numberString);
...
num3 = Convert.ToInt32(numberString);

but not values of Room instance. To easily fix that just change to:

aRoom.Length = Convert.ToInt32(numberString);
...
aRoom.Width = Convert.ToInt32(numberString);
...
aRoom.Height = Convert.ToInt32(numberString);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

It looks like you expect this code:

aRoom.Length = num1;

to somehow bind the .Length property the num1 variable. This does not happen. Only the value of the num1 variable is assigned. No reference is maintained between the two variables. Each is free to change independently of the other. The same is true for the other variables you're dealing with.

To fix it, assign the result of the Convert.ToInt32() calls directly to the object properties:

aroom.Length = Convert.ToInt32(numberString);

Note the code sometimes seem to behave the way you expect when you're dealing with reference types rather than value types. In that situation, both variables can end up referring to the same object.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794