4 Replies - 411 Views - Last Post: 27 April 2011 - 04:21 PM Rate Topic: -----

#1 d3rkn3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Getting a variable to pass through a method and a contructor

Posted 27 April 2011 - 03:44 PM

Hello!

This will be easier if I show you my code first:

My Square class
class Square
    {
        private double area;
        private int sideLength;

        public double Area
        {
            get
            {
                return area;
            }

        }

        public int SideLength
        {
            get
            {
                return sideLength;
            }
        }
        public Square(int sideLength)
        {
            SquareArea();
        }
        private void SquareArea()
        {
            area = Math.Pow(sideLength, 2);
        }
    }
}

Main Method()
class DemoSquares
    {
        static void Main(string[] args)
        {
            Square[] Square1 = {new Square(1), new Square(2), new Square(3), new Square (4),
                                   new Square(5), new Square(6), new Square(7), new Square(8),
                                   new Square(9), new Square(10)};

            for (int x = 0; x < Square1.Length; ++x)
                Square1[x] = new Square(6);

            Console.WriteLine("area is {0}",Square1[1].Area);

            Console.ReadKey();
        }
    }
}


sideLength is unused.

What I am having trouble with is getting my private sideLength passed to my SquareArea method and then to my Square constructor. Right now my ouput reads: "area is 0" no matter what part of the array I call in my WriteLine method. Ultimately, I would appreciate any knowledge as to why sideLength is automatically being assigned to zero, I know it says it is unused but I dumped it into my method and consequently my constructor. In this program my public property "SideLength" must be read only.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a variable to pass through a method and a contructor

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Getting a variable to pass through a method and a contructor

Posted 27 April 2011 - 03:55 PM

class Square
    {
        private double area;
        private int sideLength;


This sideLength belongs to a different variable scope

       public double Area
        {
            get
            {
                return area;
            }

        }

        public int SideLength
        {
            get
            {
                return sideLength;
            }
        }


to this one. This is local to the constructor.
   public Square(int sideLength)
        {
            SquareArea();
        }


It is completely different to the member variable of the same name.


        private void SquareArea()
        {
            area = Math.Pow(sideLength, 2);
        }
    }
}


The sideLength used here is the variable at class member scope level.


To help you differentiate the different scope level, that you prefix the somehow.
For member variable I use and underscore.
Int _sideLength = 0;


Then in the constructor I'll assign the value passed in to the member variable.

So the class would look similar to this.
class Square
    {
        private double area;
        private int _sideLength ;

        public double Area
        {
            get
            {
                return area;
            }

        }

        public int SideLength
        {
            get
            {
                return _sideLength ;
            }
        }
        public Square(int sideLength)
        {
            _sideLength = sideLength;
            SquareArea();
        }
        private void SquareArea()
        {
            area = Math.Pow(_sideLength, 2);
        }
    }
}


This post has been edited by AdamSpeight2008: 27 April 2011 - 03:56 PM

Was This Post Helpful? 2
  • +
  • -

#3 d3rkn3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Re: Getting a variable to pass through a method and a contructor

Posted 27 April 2011 - 04:06 PM

Thank you VERY much! Sadly, I've been working on this for quite some time thinking it may have been my logic. Is it common practice to underscore private varibles? Also, I just want to be clear on one other thing. _sideLength is my private variable, SideLength is my private _sideLength made public through my property and int SideLength (in my constructor) is a brand new variable that my constructor uses to obtain _sideLength? Again thank you.. so much.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Getting a variable to pass through a method and a contructor

Posted 27 April 2011 - 04:19 PM

That's correct.
Where a variable have the same name, the inner most in terms of scope is the dominant one.
Was This Post Helpful? 2
  • +
  • -

#5 d3rkn3ss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Re: Getting a variable to pass through a method and a contructor

Posted 27 April 2011 - 04:21 PM

Thank you, this is all starting to make a lot more sense now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1