473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Protected keyword

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
7 4399
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
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
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
12
by: Chris | last post by:
Hi I am trying to create a base class with the Protected keyword Protected MustInherit Class MyBaseClas .. .. End Clas And I got an error saying something like the Protected keyword...
1
by: herc | last post by:
Here is what MSDN says about the constructor that is needed when implementing the ISerializable interface: "The ISerializable interface implies a constructor with the signature constructor...
4
by: Tina | last post by:
This is an issue regarding what exactly is accomplished by using "Protected" when defining a variable. It seems it does much more than just applying Protected status to a variable. I have an...
7
by: atomik.fungus | last post by:
Hi, im having problems compiling some code, and i dont understand why. I've made a Matrix class for any kind of data and now im implementing an inherited class with all the mathematical stuff...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
5
by: =?Utf-8?B?a21ybmVlZHNuZXRoZWxw?= | last post by:
I have the class structure shown below. When I compile I receive the following erros: 'B.B()' is inaccessible due to its protection level 'C.C()' is inaccessible due to its protection level I...
2
by: JB | last post by:
Hi All, I've just experienced a "strange" behaviour with inheritance and the Protected keyword. Can someone please shed a light on this? Is this the normal behaviour? Class Base Protected...
1
by: Sivakoti | last post by:
how can we use protected keyword?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.