473,396 Members | 2,121 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.

Access to protected members

Hi,

I am new to C# & .NET.

I have a class A which contains a protected member M that I would like to
access. If I derive my own class B from A, I can access M from within the
class, and also expose it via a property. The problem is that an instance
of B cannot be used where an instance of A is expected (unsafe cast), so it
does not solve my problem. Of course I can write a soution that does not
need access to M, but I am trying to find out if there are other
alternatives.

Could anyone give me some recommendations for situations like the above?

Thanks,
Laban

Ps. I use .NET 1.1
Nov 17 '05 #1
5 1860
First don't use protected data members, but protected function members
are OK. Second, consider using polymorphism as in:

using System;

namespace TestVirtual
{
/// <summary>
/// Summary description for Class1.
/// </summary>
abstract public class Class1
{
private int i=1;

abstract public int MyVersionOfInt{get;}
protected int GetI()
{
return i;
}
}
public class MyDerived :Class1
{
public override int MyVersionOfInt
{
get
{
return GetI()*2;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 c1= new MyDerived();
System.Console.WriteLine(c1.MyVersionOfInt);
System.Console.ReadLine();
}
}

}
Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
The problem is that an instance
of B cannot be used where an instance of A is expected (unsafe cast)
This doesn't sound right too me. Thats basically the whole point of
polymorphic substitution and forms the underlying mechanics of the well used
Factory pattern. Sure you havent got that the wrong way round?

RR

"Laban" <no****@dev.null> wrote in message
news:11*************@corp.supernews.com... Hi,

I am new to C# & .NET.

I have a class A which contains a protected member M that I would like to
access. If I derive my own class B from A, I can access M from within the
class, and also expose it via a property. The problem is that an instance
of B cannot be used where an instance of A is expected (unsafe cast), so it does not solve my problem. Of course I can write a soution that does not
need access to M, but I am trying to find out if there are other
alternatives.

Could anyone give me some recommendations for situations like the above?

Thanks,
Laban

Ps. I use .NET 1.1

Nov 17 '05 #3
Laban wrote:
I have a class A which contains a protected member M that I would like to
access. If I derive my own class B from A, I can access M from within the
class, and also expose it via a property. The problem is that an instance
of B cannot be used where an instance of A is expected (unsafe cast), so
it does not solve my problem. Of course I can write a soution that does
not need access to M, but I am trying to find out if there are other
alternatives.

Could anyone give me some recommendations for situations like the above?


Well, protected members are made protected because it's a design decision
that they should NOT be available to outside classes. So probably, if you
find yourself needing that kind of access, there's either something wrong
with the class design or you are doing something that the class designer
didn't want you to do. You should find out why there's this
incompatibility between the class design and your needs and fix it, not
work around it.

That said, of course you can use an instance of a derived class where a
base class instance is expected. Like here:

class A {
protected int M;
}

class B : A {
public int PublicM { get { return M; } }
}

class C {
public void DoSomething(A a) {
// Now we have an A that may of course be a B in reality

// Doing something with a.PublicM is not immediately
// possible here, I suspect that's what you mean.
}
}
...
B b = new B();
C c = new C();

c.DoSomething(b);
...

As the comment in the method says, if you get an A passed in and you want
to access that new member you introduced in the derived B, you can't
immediately do that because the type of the variable you have is still an
A, not a B. If you actually get all types of objects passed in that are
derived from A, and you want to access the derived class members, you
could start like this:

public void DoSomething(A a) {
B b = a as B;
if (b != null) {
// Now do something with b.PublicM
}
}

If there are various derived types of A that you want to deal with, the
code in that method can consist of loads of if statements... have a look
at the visitor pattern (just search Google) for a way to better deal with
that kind of situation.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #4
Thanks a lot for the help, guys!! That clarified things.

Laban


Nov 17 '05 #5
Thanks for the replies, it helped me get it working (and, yes, I had it
backwards).

Laban
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
7
by: Andy Ward | last post by:
Given the following code: class A { protected: int pro; }; class B : public A { public:
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
3
by: Pete | last post by:
Hello Can anyone provide advice/links on somewhere to get a definitive explanation with Examples of how "protected internal" differs from "protected", "internal" and the other access modifiers ...
3
by: Nathan Wiegman | last post by:
Hi, Why must derived classes access protected base members through an instance of a derived class? The following code would work in C++, why doesn't it in C#? public class A { protected...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.