derive class from protected class in base class 
Author Message
 derive class from protected class in base class

Hi!

First, an example:

Public Class A
    Protected Class B
    End Class

    Protected Overridable Function CreateB() As B
        Return New B()
    End Function
End Class

Public Class C
    Inherits A

    Protected Class D
        Inherits B    'or A.B    '<=== error
    End Class

    Protected Overrides Function CreateB() As B
        Return New D()
    End Function
end Class

Purpose: Somewhere in A, CreateB is called. CreateB is only there to create
a B-object. Classes derived from A can override CreateB and return an object
that is inherited from B. So class C isn't limited to the predefined
B-class.

Problem: Class D can't be derived from class B. Compiler error:

    "A.B" ist Protected and not accessible in this context

Why? I can't find a logical explanation.

Armin



Thu, 10 Jun 2004 00:44:02 GMT  
 derive class from protected class in base class
Armin,

Do you mind letting me know why you are doing this? It is an issue with the
VB compiler. My interest was in regard to a .NET infrastucture issue, but
would like to be able to pass on real world reasons to want to do this (I
can imagine some, but what were you trying to do?). You can use email if you
prefer.

The unfortunate answer to your question is that you will have to use C# to
accomplish this in .NET V1.

Kathleen



Thu, 10 Jun 2004 06:51:18 GMT  
 derive class from protected class in base class

Hi Kathleen,

Quote:
> Do you mind letting me know why you are doing this? It is an
> issue with the VB compiler. My interest was in regard to a .NET
> infrastucture issue, but would like to be able to pass on real
> world reasons to want to do this (I can imagine some, but what
> were you trying to do?). You can use email if you prefer.

> The unfortunate answer to your question is that you will have to
> use C# to accomplish this in .NET V1.

When you're asking for the issue, I wonder, why I've already provided an
explanation, not to get this question:  ;-)
<quote>
Purpose: Somewhere in A, CreateB is called. CreateB is only there to create
a B-object. Classes derived from A can override CreateB and return an object
that is inherited from B. So class C isn't limited to the predefined
B-class.
</quote>

The general explanation in an example:

A
+--reference to--> B

This is the given structure. Now I want to widen the features
of class A. So I write class C that inherits from A. Now the issue: The
B-objects that are created and referenced in A are not powerful enough for
class C. That's the reason for class D: It's inherited from B and contains
improvements or changes and so on. So far, so good. The problem in such a
situation is always: B-objects are created within the base class(!).
Therefore C doesn't have a chance to have references to D-objects
*instead of* B-objects.

When I create an overridable function to create a B-class, successor of
A-classes (here: C) do have the chance to instanciate there own objects that
are derived from D. The A class wouldn't have a problem with these D-objects
because they are derived from B (polymorphism).

I haven't found another way to do something like "inheriting a tree".
Inheriting the root object is no problem but creating further developed
objects that are referenced by the root, isn't that easy. IMHO it can only
be achieved by moving the point of creation to the derived class because the
base class does not even know the D-class.

Of course, I'd be very happy about any other approach.

I hope, this is a better explanation now.

Armin



Thu, 10 Jun 2004 19:27:12 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. StatusBarPanel Collection containing panels based on different classes derived from StatusBarPanel class

2. how to raise base class event and method from the derived child object

3. Using Class Builder Utility to create a base class (VB6)

4. Dim obj As New Class crt Dim Obj As Class = New Class

5. C# IDE doesn't show base class members for derived class?

6. Problem: Calling Events from Derived Classes

7. Can't add a ComponentModel.Component derived class

8. Hiding Properties in a Derived Class?

9. hiding an event in a derived class

10. Making a DictionaryBase derived class case-insensitive.

11. Delayed initialisation in derived class, bug?

12. can I supress some properiets or method in derived class

 

 
Powered by phpBB® Forum Software