Protected keyword

G

Guest

HI, I'm using the protected keyword in a class to make a method accessible to
a child that inherits from it so as to be able to call the protected method
from the child class.

I can't seem to be able to access it from either the base class or the class
that inherits from it. Here is a sample of the code:

Any ideas would be most helpful.

Thanks in advance
Ant

public delegate void myDelegate(string message);
public class Person // The base class
{
// Constructor
public Person()
{
this.Deceased +=new myDelegate(Person_Deceased);
}

// Event
public event myDelegate Deceased;

// method to raise the deceased event
protected void RaiseEvent(string message)
{
this.Deceased(message);
}

// Event Handler
private void Person_Deceased(string message)
{
MessageBox.Show(message);
}

}

// class that inherits the Person class
public class Employee:person
{
}

private void buttonClickEvent()
{
Employee emp = new Employee();
emp.RaiseEvent("Psycho"); // Get compile time inaccessible error here..
}
 
J

Joanna Carter [TeamB]

"Ant" <[email protected]> a écrit dans le message de (e-mail address removed)...

| HI, I'm using the protected keyword in a class to make a method accessible
to
| a child that inherits from it so as to be able to call the protected
method
| from the child class.

| private void buttonClickEvent()
| {
| Employee emp = new Employee();
| emp.RaiseEvent("Psycho"); // Get compile time inaccessible error here..
| }

Protected means accessible from code *within* derived classes, not from code
external to them. Your example is trying to talk to a protected method of an
*instance* of the derived class instead of within the derived class.

Joanna
 
G

Guest

Only the Employee class internally can see the base class RaiseEvent method.
It is exposed through an instance of the class.

So if you did something like the following it would work:

public class Employee : Person
{
private bool heartIsBeating;

public void HoldBreath(){
while(true){
if(!heartIsBeating){
base.RaiseEvent("Heart stopped, deceased!");
break;
}
}
}
}
 
G

Guest

It is *NOT* exposed through an instance of the class. It's still early for me
this morning. :)
 
G

Guest

Thanks Demetri but...

The problem I'm having is with the protected keyword exposing a member.
Another example:

public class Person
{

protected void ProtectedMethod()
{
MessageBox.Show("Can see it");
}

}

// inherits the person class
public class Employee:person
{
}

private void button_Click(object sender, System.EventArgs e)
{
Employee e = new Employee();

// Should be able to access e.ProtectedMethod here but can't??

e. // nothing here but object methods
}

Why can't I see the ProtectedMethod event?

Thanks again

Ant
 
C

Chris Dunaway

Ant said:
HI, I'm using the protected keyword in a class to make a method accessible to
a child that inherits from it so as to be able to call the protected method
from the child class.
// class that inherits the Person class
public class Employee:person
{
}

private void buttonClickEvent()
{
Employee emp = new Employee();
emp.RaiseEvent("Psycho"); // Get compile time inaccessible error here..
}

I presume that this buttonClickEvent is in a form. As per your initial
statement, the protected keyword means that the member is available
inside the class that defines it and in derived classes. You are not
calling it from either place.

In your buttonClickEvent, you are no longer "inside the employee
class". You are trying to access it as a public member outside the
derived class.

You would need something like this to access the protected member:

Public Class Employee : Person
{
public void Foo()
{
base.RaiseEvent("Psycho"); //This is using it "inside" the
derived class
}
}

private void buttonClickEvent()
{
Employee emp = new Employee();
emp.Foo();
}
 
J

Joanna Carter [TeamB]

"Ant" <[email protected]> a écrit dans le message de (e-mail address removed)...

| The problem I'm having is with the protected keyword exposing a member.

Both Demetri and I have answered your question in two different ways, but
saying the same thing. Chris has also given yoiu an example, do you still
not understand ?

You can only see protected members in classes that derive from the declaring
class.

You *cannot* see protected members in any other classes, including a form
class.

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top