473,385 Members | 1,356 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,385 software developers and data experts.

Inheritance puzzle

Hi all,

I have hit a situation that has me stumped. I have two class
libraries called Base and Derived. They look like this:

' BASE
Public Class Class1
Public Overridable Sub DoSomething()
With New Class2()
.ShowMessage()
End With

MsgBox(Me.GetType.Assembly.FullName)
MsgBox(Me.GetType.Namespace)
End Sub
End Class
Public Class Class2
Public Overridable Sub ShowMessage()
MsgBox("I am Base.Class2")
End Sub
End Class

'DERIVED (this library references BASE)
Public Class Class1
Inherits Base.Class1

Public Overrides Sub DoSomething()
MyBase.DoSomething()
End Sub
End Class
Public Class Class2
Inherits Base.Class2

Public Overrides Sub ShowMessage()
MsgBox("I am Derived.Class2")
End Sub
End Class

So basically Derived.Class1 inherits from Base.Class1 and
Derived.Class2 inherits from Base.Class2. (Obviously this is a
simplified example and there would actually be more stuff in
DoSomething.)

My test program for this is a windows application that contains this:

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
With New Base.Class1()
.DoSomething()
End With

With New Derived.Class1()
.DoSomething()
End With
End Sub

When I run this, the call to Base.Class1.DoSomething shows the message
"I am Base.Class2" as expected. My problem is that the call to
Derived.Class1.DoSomething shows the same thing instead of the "I am
Derived.Class2" that I was hoping for. So in both cases, it is using
Base.Class2. What I don't understand is why. The message boxes that
show the assembly and namespace both show "Derived" on the second
call. When it goes to create Class2, I guess I expected it to look in
the current assembly/namespace first (even though the actual running
code is in Base)

Am I missing something here?

For what I'm trying to accomplish, passing instance of the derived
classes into the base classes isn't an option. I did figure out a way
around this which works but seems really hokey (and probably isn't
very effecient) Instead of using

With New Class2

I have to use:

With Activator.CreateInstance(Me.GetType.Assembly.GetTy pe(Me.GetType.Namespace
& ".Class2"))

Any thoughts on this will be greatly appreciated.

Thanks,
Glenn
Nov 21 '05 #1
1 1432
NM
Hi,

You should overrides the method DoSomething in your DERIVED class class1 to
create DERIVED class class2 and call its method; like that :

'DERIVED (this library references BASE)
Public Class Class1
Inherits Base.Class1

Public Overrides Sub DoSomething()
With New Class2()
.ShowMessage()
End With

MsgBox(Me.GetType.Assembly.FullName)
MsgBox(Me.GetType.Namespace)
End Sub
End Class
Regards;


"Glenn Cory" <gc***@igiles.net> a écrit dans le message de
news:fc**************************@posting.google.c om...
Hi all,

I have hit a situation that has me stumped. I have two class
libraries called Base and Derived. They look like this:

' BASE
Public Class Class1
Public Overridable Sub DoSomething()
With New Class2()
.ShowMessage()
End With

MsgBox(Me.GetType.Assembly.FullName)
MsgBox(Me.GetType.Namespace)
End Sub
End Class
Public Class Class2
Public Overridable Sub ShowMessage()
MsgBox("I am Base.Class2")
End Sub
End Class

'DERIVED (this library references BASE)
Public Class Class1
Inherits Base.Class1

Public Overrides Sub DoSomething()
MyBase.DoSomething()
End Sub
End Class
Public Class Class2
Inherits Base.Class2

Public Overrides Sub ShowMessage()
MsgBox("I am Derived.Class2")
End Sub
End Class

So basically Derived.Class1 inherits from Base.Class1 and
Derived.Class2 inherits from Base.Class2. (Obviously this is a
simplified example and there would actually be more stuff in
DoSomething.)

My test program for this is a windows application that contains this:

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
With New Base.Class1()
.DoSomething()
End With

With New Derived.Class1()
.DoSomething()
End With
End Sub

When I run this, the call to Base.Class1.DoSomething shows the message
"I am Base.Class2" as expected. My problem is that the call to
Derived.Class1.DoSomething shows the same thing instead of the "I am
Derived.Class2" that I was hoping for. So in both cases, it is using
Base.Class2. What I don't understand is why. The message boxes that
show the assembly and namespace both show "Derived" on the second
call. When it goes to create Class2, I guess I expected it to look in
the current assembly/namespace first (even though the actual running
code is in Base)

Am I missing something here?

For what I'm trying to accomplish, passing instance of the derived
classes into the base classes isn't an option. I did figure out a way
around this which works but seems really hokey (and probably isn't
very effecient) Instead of using

With New Class2

I have to use:

With Activator.CreateInstance(Me.GetType.Assembly.GetTy pe(Me.GetType.Namespace & ".Class2"))

Any thoughts on this will be greatly appreciated.

Thanks,
Glenn

Nov 21 '05 #2

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

Similar topics

7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
6
by: matthew_lancashire | last post by:
First thing, I am using Framework 2.0 and VS2005 Beta 2. I have a base class that implements an interface and has a must override sub that implements an interface to a sub in the ompleneted class...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
9
by: James Crosswell | last post by:
I'm not sure if I'm going about this the right way - it may be that Generics might be able to help me out here... but here goes: I have three classes as follows class BaseEdit class WidgetEdit:...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.