By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,982 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,982 IT Pros & Developers. It's quick & easy.

Protected keyword

P: n/a
Ant
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..
}

May 18 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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;
}
}
}
}
--
-Demetri
"Ant" wrote:
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..
}

May 18 '06 #2

P: n/a
It is *NOT* exposed through an instance of the class. It's still early for me
this morning. :-)
--
-Demetri
"Demetri" wrote:
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;
}
}
}
}
--
-Demetri
"Ant" wrote:
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..
}

May 18 '06 #3

P: n/a
"Ant" <An*@discussions.microsoft.com> a écrit dans le message de news:
6C**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 18 '06 #4

P: n/a
Ant
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


May 18 '06 #5

P: n/a
Ant wrote:
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();
}

May 18 '06 #6

P: n/a
Exactly what I said, just in different words. :)
--
-Demetri
"Joanna Carter [TeamB]" wrote:
"Ant" <An*@discussions.microsoft.com> a écrit dans le message de news:
6C**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

May 18 '06 #7

P: n/a
"Ant" <An*@discussions.microsoft.com> a écrit dans le message de news:
DD**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.