Hello,
Having trouble with a program for school. Prof gave a shell that has color declared as static. Here is what I have so far.

class MyRectangle
     { 
          private double width = 1;          //private class instance fields
          private double height = 1;
          private static String color = "white";

          public MyRectangle(){ }     //empty constructor

          public MyRectangle(double widthParam, double heightParam, String colorParam)
          {
              width = widthParam;            // passed in values assigned 
              height = heightParam;           //to class variables
              color = colorParam;
          }

          public double getWidth(double widthParam)
          {
              return width;
          }

          public double getHeight(double heightParam)
          {
              return height;
          }

          public static String getColor(String colorParam)
          {
              return MyRectangle.color;
          }

          public double findArea()
          {
              double area = width * height;
              return area;
          }

          public void OutputArea()
          {
              Console.WriteLine("Width = {0}\n" + "Height = {1}\n" +
                 "Color = {2}\n" + "Area = {3}\n",
                 width, height, color, width*height);
          }

     }//end rectangle class

     class rectangle_assignment //wrapper class for main
     {

        static void Main(string[] args)
        {
            MyRectangle shape1 = new MyRectangle(3, 5, "red"); //instantiates object
           
            MyRectangle shape2 = new MyRectangle(2, 5, "yellow"); //instantiates object

            shape1.findArea();
            shape1.OutputArea();
            Console.WriteLine();
            shape2.findArea();
            shape2.OutputArea();    

        }//end main

Write the code in main to create two MyRectangle objects. Assign a width and height to each of the two objects using the constructors. Assign the first object the color red, and the second, yellow. Display all properties of both objects including their area. The output should look something like this:
Width = 3
Height = 5
Color = red
Area = 15

Width = 2
Height = 5
Color = yellow
Area = 10
Press any key to continue . . .

I have everything working and printing the way it is supposed to . . . problem is, the color is yellow for both shapes. I know it has something to do with static. I have debugged and the first shape has its color as red until the yellow is passed, then both shapes are yellow.

Please help. Thanks.

Hello,
yeah, static word might be confusing for some time. But it's not that hard .. Static members has a class level, rather then object one. To understand it better, let's look at example. Imagine a form for making cookies ..

Your form is your class. And your cookies are your objects.
If you change your form once, it would affect all the children .. so the line:

color = colorParam;

means "May all MyRectangle objects would have a given color" .. all the objects, no matter if they were created before or after this call (of course, this for the case, when the color variable is static).

Ok, and now, correct your code, knowing all this .. or ask if something left unclear :)

commented: He! Nice explanation :) +8
commented: Helpful! +11

Thank you for responding! I still did not quite follow how you would suggest I change the code from what you wrote. But someone in class made a comment that lit the light bulb above my head. So what I did was move where a line of code was as follows:

static void Main(string[] args)
        {
            MyRectangle shape1 = new MyRectangle(3, 5, "red"); //instantiates object
            shape1.findArea();
            shape1.OutputArea();
            Console.WriteLine();
           
            MyRectangle shape2 = new MyRectangle(2, 5, "yellow"); //instantiates object

            
            shape2.findArea();
            shape2.OutputArea();    

        }//end main

That did the trick. Is that what you meant?

commented: Anytime it's nice to see the one, who want to learn :) +3

Exactly!
I was suggesting you to be a kinda photographer .. you're arranging the objects in some manner, then taking a shot, then making some permanent changes to your scene and making another shot. Good job! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.