Another question on Inheritance

J

Joe Fallon

Public MustInherit Class My3rdLevelClass

Inherits GeneratedClass

Public Shared Function GetSomething() As String

Return "Something"

End Function

In the sample above I have declared the class as MustInherit.

I have questions about the scope of the GetSomething method.
I was able to call it directly from a class in the same project.
What should it be set to in order to allow a class that inherits it the
ability to "use it" but that does not allow anyone to call it directly?
 
F

Fergus Cooney

Hi Joe,

That's the Protected keyword you're after there. For methods
that are available to the current class and all derived classes only.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Joe Fallon said:
Public MustInherit Class My3rdLevelClass

Inherits GeneratedClass

Public Shared Function GetSomething() As String

Return "Something"

End Function

In the sample above I have declared the class as MustInherit.

I have questions about the scope of the GetSomething method.
I was able to call it directly from a class in the same project.
What should it be set to in order to allow a class that inherits it the
ability to "use it" but that does not allow anyone to call it directly?

Set its modifier to 'Protected'.
 
J

Joe Fallon

Fergus,
I tried using Protected (and Protected Friend) but it didn't seem to work
right.

As you recall I have 4 levels of classes.
When they are all Public Shared, they work fine.

I just use syntax like:
4thLevelClassName.SomeMethod
(SomeMethod is Public and is in Level 1,2 or 3)

But when I made SomeMethod a Protected function, the 4th level could not
"see" it even though it inherited it.

I think I am missing something here.
Can you enlighten me?
 
F

Fergus Cooney

Hi Joe,

It depends on what you mean by 'see'. Do you mean that
1) Intellisense can't see it or
2) Level4 can't access it (within itself) and gives a compiler
warning, or
3) users of the Level4 class can't access it?

It would be handy to post some code as well.

Cheers,
Fergus
 
J

Joe Fallon

Fergus,
The first 2 for sure.

There was no intellisense for the method and the compiler underlined the
method and said it could not be accessed because it was Private.

Sample code:

Level 1
Protected MustInherit Class Base

Protected Shared Function DoSomething(ByVal Source As String) As String

End Function


================================================
Level 2
Protected MustInherit Class GeneratedStuff

Inherits Base

Protected Shared Function DoSomethingElse(ByVal Source As String) As String

End Function


================================================
Level 3
Protected MustInherit Class HandCodedStuff

Inherits GeneratedStuff

Protected Shared Function IWroteThis(ByVal Source As String) As String

End Function


================================================
Level 4 (implements same functionality as Level 3 but also allows customized
changes by overriding methods or creating new ones that are specific to a
single client. Level 3 functionality is for all clients.)
Public MustInherit Class FinalLevel

Inherits HandCodedStuff

Public Shadows Function DoSomethingElse(ByVal Source As String) As String

End Function
 
F

Fergus Cooney

Hi Joe,

Unfortunately that's the outline-code that you posted before showing what
you'd like to do. I need to see how you're actually putting it into practice
so that I can spot where it's going wrong.

With the code, could you do me a favour? Copy it from your project into
NotePad. Then cut it from there and paste it into the message. This should
prevent all the double lines (which I have to remove before I can compile the
code). If it's a lot of code, I'd prefer an attachment - zip or txt file - as
this will preserve the line lengths as well.

Regards,
Fergus
 
J

Joe Fallon

I don't have a valid code sample. handy.
Since it didn't work I re-wrote it.

Perhaps tomorrow I can put together a small project.
If so, I will zip it up and send it to ... where?

Thanks for your willingness to help!
 
F

Fergus Cooney

Hi Joe,

Lol, I'm happy to help, as the saying goes. :))

I'd prefer it to be sent here as it's easier for me. It also allows anyone
else interested to have a look. If it's only a few K that's fine - many
messages are longer. You could post in-line code but you're kindly making it
smaller by zipping. That seems fair to me

If you feel uncomfortable with that, though, my email address
(e-mail address removed) should be working (it gets enough spam!). But because it <is>
a spam destination address, I only check it when I know there's something
waiting there. So you'll have to send it there then let me know here. (Which
is one reason why I'd prefer the zip here). LOL. Trust your instincts. ;-)

Regards,
Fergus
 
J

Joe Fallon

I didn't forget.
Swamped at work.
I have a business trip coming up too so I may not get to it for a week or
so.
It is at the top of my list when I get time to work on it though!

I will send the e-mail and post a note here.
I dislike attachments in Newsgroups unless they have Binary in their title.
It is unfair to those users who don't want to waste the time and bandwidth
to download files.

Thanks!
 
J

Joe Fallon

Fergus,
I found some time today and built a sample and e-mailed it to you.
Traveling tomorrow til Thursday.
Thanks!
 

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