Hiding items in base classes

  • Thread starter Thread starter OpticTygre
  • Start date Start date
O

OpticTygre

If I have a Class "B" that Inherits Class "A", and my application uses class
"B", is there a way that I can keep from exposing properties, subs, and
events in the Class "A" base class from the application?

For example, if Class "A" has a "StateChanged" event that I don't want the
application to see, is there a way to hide exposure to that event in Class
"B"?

Thanks for any help.

-Jason
 
Declare the event as being protected or friend, depending on your exact
needs.
 
So in Class "B" I would do something like:

Protected Shadows Event StateChanged(Sender as Object, Args as
StateChangedArgs)

Or

Protected Shadows Property SomeProperty as String

Is that right?
 
No, that doesn't work, at least for properties.

Class "A" has a property, BindIP that is public and overridable.
Class "B" Inherits class "A", thereby inheriting the BindIP property
My application uses Class "B", and not Class "A"
I don't want my application to know that this BindIP property exists.

If I try to say something like "Protected Overrides Property BindIP as
String", the code produces an error, because Class A defines BindIP as
Public, and I'm trying to redefine it as Protected in Class B.

How can I get around that, and keep it hidden from my application?

-Jason
 
BindIP has to be declared protected in A.

OpticTygre said:
No, that doesn't work, at least for properties.

Class "A" has a property, BindIP that is public and overridable.
Class "B" Inherits class "A", thereby inheriting the BindIP property
My application uses Class "B", and not Class "A"
I don't want my application to know that this BindIP property exists.

If I try to say something like "Protected Overrides Property BindIP as
String", the code produces an error, because Class A defines BindIP as
Public, and I'm trying to redefine it as Protected in Class B.

How can I get around that, and keep it hidden from my application?

-Jason
 
Which is no good, as I don't have access to the code in A. It is a
pre-compiled dll.
 
No.

"When one type inherits from another, it gets all of the members. You can
hide members in your inherited type, but you can't get rid of them."
- Microsoft documentation.

You can 'hide' members with overriding and shadowing.

Overriding:
"By default, a derived class inherits methods from its base class. If an
inherited property or method needs to behave differently in the derived
class it can be overridden; that is, you can define a new implementation of
the method in the derived class."
- Microsoft documentation.

Shadowing:
"When two programming elements share the same name, one of them can hide -
that is, shadow - the other one. In such a situation, the shadowed element
is not available for reference; instead, when your code uses the shared
name, the Visual Basic compiler resolves it to the shadowing element."
- Microsoft documentation.
 
Then you are probably out of luck.

The best you can do, as far as i know, is make it 'protected shadows'.

However, someone can just do this:

CType(myBInstance, A).BindIP
 

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

Back
Top