Access to protected members

L

Laban

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
 
J

Jeff Louie

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
 
R

Roger Rabbit

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
 
L

Laban

Thanks for the replies, it helped me get it working (and, yes, I had it
backwards).

Laban
 
O

Oliver Sturm

Laban said:
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
 

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