run base and inherited method

A

acadam

Public MustInherit Class A
Public Sub method1()
...
End sub
End Class

Partial Class B
Inherits A

Public Sub method1()
...
End sub
End Class

I create an instance of B, but when I call methodA, I would like that
1. would be executed method1 - base class A
2. then would be executed method1 - derived class B

is it possible? how?

tnx very much
 
P

Phill W.

acadam said:
Public MustInherit Class A
Public Sub method1()
...
End sub
End Class

Partial Class B
Inherits A

Public Sub method1()
...
End sub
End Class

The compiler should be warning you about Shadowing method1(). IMHO,
Shadowing is a Bad Thing and should generally be avoided.

If you want to be able to change the behaviour of a method in a derived
class then mark the method as Overridable in the base class and
overrides in the derived class, as in:

Mustinherit Class A
Overridable Sub method1()

Class B
Inherits A
Overrides Sub method1()
I create an instance of B, but when I call methodA, I would like that
1. would be executed method1 - base class A
2. then would be executed method1 - derived class B

is it possible? how?

Yes; I don't know the "proper" term for it, but I call it "extending" a
method:

When you override a method, the Framework "unplugs" the original
implementation (from the base class) and "plugs in" your replacement
implementation (from your derived class) but the original is still
hanging around [somewhere] and you /can/ call it:

Overrides Sub method1()
' class the vase implementation
MyBase.method1()
' Now you can do something extra
End Sub

Or, perhaps, you might want to do something else /first/, and then go
back to the base implementation (this is a common model when handling
events in Control-derived classes).

Overrides Sub OnDoubleClick(e as EventArgs)
If Not Me.IgnoreDoubleClick Then
MyBase.OnDoubleClick( e )
End If
End Sub

HTH,
Phill W.
 
A

Armin Zingler

acadam said:
Public MustInherit Class A
Public Sub method1()
...
End sub
End Class

Partial Class B
Inherits A

Public Sub method1()
...
End sub
End Class

This can not be compiled, but see below....
I create an instance of B, but when I call methodA,
method1?

I would like
that
1. would be executed method1 - base class A
2. then would be executed method1 - derived class B

is it possible? how?

Public MustInherit Class A
Public Overridable Sub method1()
MsgBox("A")
End Sub
End Class

Partial Class B
Inherits A

Public Overrides Sub method1()
MyBase.method1()
MsgBox("B")
End Sub
End Class


Armin
 
A

Academia

Excuse me for jumping in.
What would you do if you had a UserControl that you wanted to have a Text
property that did something other than what the UserControl's Text property
does. Is that an exception or is there a better way than Shadowing?


thanks

">
 
P

Phill W.

Academia said:
What would you do if you had a UserControl that you wanted to have a Text
property that did something other than what the UserControl's Text property
does.

IIRC, the UserControl's Text Property is Overridable (VB'2003):

Public Class CustomControl
Inherits System.Windows.Forms.UserControl
.. . .
Public Overrides Property Text() _
As String

HTH,
Phill W.
 
A

Academia

I'll change now.

thanks

Phill W. said:
IIRC, the UserControl's Text Property is Overridable (VB'2003):

Public Class CustomControl
Inherits System.Windows.Forms.UserControl
. . .
Public Overrides Property Text() _
As String

HTH,
Phill W.
 

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