How to execute parent class functions and then child's

  • Thread starter Thread starter msxkim
  • Start date Start date
M

msxkim

How to execute functions in the parent class first and then functions
in the child class? For example, I have a parent class with functions
'ONE' and 'TWO' and child class has a function 'THREE'. How should I
declare classes so that all three functions are executed when child
class is called?

Class Parent
public function ONE
'code
end fuction

public function TWO
'code
end fuction
end class

Class Child
Inherites Parent

public function THREE
'code
end fuction
end class
 
How to execute functions in the parent class first and then
functions in the child class? For example, I have a parent class
with functions 'ONE' and 'TWO' and child class has a function
'THREE'. How should I declare classes so that all three functions
are executed when child class is called?

Class Parent
public function ONE
'code
end fuction

public function TWO
'code
end fuction
end class

Class Child
Inherites Parent

public function THREE
'code
end fuction
end class

public function THREE
ONE
TWO
end fuction


That's what you need?

Armin
 
Hi,

I don't know if I understood you.

However you can set in the Sub New of your parent class

\\\
Public Sub New
ONE
TWO
End Sub
///

I hope this was what you where after

Cor
 
After some thought, this is what I would like to have. I am sorry to
both of you.
I have a parent class and child class. When I execute function ONE
from child class, is it going to executes parent's code plus child's
code? How should I declare or inherite a classe so that it executes
parent's code and then child's code?

Example:
Class Parent

public function ONE
'Parent Code
end fuction
end class


Class Child
Inherites Parent

public function ONE
'Plus Child Code
end fuction
end class




(e-mail address removed) Jun 6, 11:27 am show options

Newsgroups: microsoft.public.dotnet.languages.vb
From: (e-mail address removed) - Find messages by this author
Date: 6 Jun 2005 08:27:48 -0700
Local: Mon,Jun 6 2005 11:27 am
Subject: How to execute parent class functions and then child's
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

How to execute functions in the parent class first and then functions
in the child class? For example, I have a parent class with functions
'ONE' and 'TWO' and child class has a function 'THREE'. How should I
declare classes so that all three functions are executed when child
class is called?
 
Msxkim,

It depends if one is a function that always should be performed in that
class, than in the way as I wrote. Is it not standard in that class, than in
the way as Armin wrote.

Cor
 
After some thought, this is what I would like to have. I am sorry
to both of you.
I have a parent class and child class. When I execute function
ONE from child class, is it going to executes parent's code plus
child's code? How should I declare or inherite a classe so that it
executes parent's code and then child's code?

Example:
Class Parent

public function ONE
'Parent Code
end fuction
end class


Class Child
Inherites Parent

public function ONE
'Plus Child Code
end fuction
end class


Class Child
Inherites Parent

public function ONE
MYBASE.one
'Plus Child Code
end fuction
end class


Armin
 
I have a parent class and child class. When I execute function
ONE from child class, is it going to executes parent's code plus
child's code?

It depends. ;-)

Given these :

Class Parent
Public Sub ONE()
Debug.Writeline( "Parent.ONE" )
End Sub
End Class

Class Child
Inherits Parent
End Class

then

Dim c As New Child
c.ONE()

will display "Parent.ONE" - the child class is simply "passing on" the
Parent class' functionality.

However, changing these classes a little :

Class Parent
Public Overridable Sub ONE()
Debug.Writeline( "Parent.ONE" )
End Sub
End Class

Class Child
Inherits Parent
Public Overrides Sub ONE()
Debug.Writeline( "Child.ONE" )
End Sub
End Class

then

Dim c As New Child
c.ONE()

now displays "Child.ONE". The Child mothod replaces (a.k.a.
Overrides) the Parent method.

<--
And, just to be /really/ confusing, try this and see what you get ...

DirectCast( c, Parent ).ONE() ' ;-)
-->

To get /both/ sets of code to run, you make the Child method
call the Parent one, as in

Class Child
Inherits Parent
Public Overrides Sub ONE()
MyBase.ONE()
Debug.Writeline( "Child.ONE" )
End Sub
End Class

which (finally) displays "Parent.ONE" followed by "Child.ONE".

HTH,
Phill W.
 
Back
Top