Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
3
Reply
Setting variable values in constructor, help
Moose Pickard
Sep 20 2010 6:34 AM
1.9k
Reply
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
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
Displaying pdf file on link click
creating modules in C#