Moose Pickard

Moose Pickard

  • NA
  • 64
  • 69.5k

Setting variable values in constructor, help

Sep 20 2010 6:34 AM
Hey 

I've got this Abstract class and I want some other class to inherit from it.

  abstract class Tree {
         public string name;
 private int age;
         private int length;

         public int length {
         get { return length; }
         set { length = value; }
         }
    }

    class Oak: Tree {
        public Oak(string name, int age, int lengthInput) {
        this.name = name;
this.age = age;
        length = lengthInput; 
    }

   
The problem is with setting the value of these variables in the Oak class.
this.name = name - doesn't give me errors, because I stated in the Tree class that name is public.
this.age = age - doesn't work, it says Tree.age is inaccessible due to it's protection level.
length = lengthInput - doesn't give me errors.

So If anyone can explain to me what do these things do?
I think I maybe overprotective with my variables. When should I use private keyword? At this point I have marked all private and created get/set declarations. 


Thank you

Answers (3)