I have a two classes.This is my code:
//My Base class
public class People
{
public People()
{
}
protected string name;
protected string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
//The Child class
public class Student:People
{
private int id;
public Student()
{
}
public Student (int id, string name)
{
this.id = id;
this.Name = name;
}
public int ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
When i create instance of the Student class like the one below i can't access the NAME property from the parent class People.
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Student student1 = new Student();
student1. // only ID property is accessible
}
}
Do i make something wrong? Since Students is child class of People i expected that the NAME property should be accessible trough the Student instances.Thank you very much for the help in advance.
You're not doing anything wrong, but if you want to access
Name
via an instance of
Student
You have to declare that property public
. Otherwise, only access from within that class is allowed (not via an instance).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments