Calling parent class?

G

Guest

I have a question on calling parent class... What I want to do is: when
calling the derived class, it automatically executes the parent's method, is
it possible?

// main code:
Derived d = new Derived();
d.SomeMethod();


public class Base
{
public virtual void SomeMethod() {
Console.WriteLine "A";
}
}

public class Derived : Base
{
public override void SomeMethod() {
// this.parent.SomeMethod();
Console.WriteLine "B";
}
}

Is there a way to call the derived class and then both "A" and "B" will be
printed out?
 
R

Richard Blewett [DevelopMentor]

public class Derived : Base
{
public override void SomeMethod() {
base.SomeMethod(); // note lower case 'b'
Console.WriteLine "B";
}
}

base is a keyword that allows you to invoke a method on your direct base class

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

I have a question on calling parent class... What I want to do is: when
calling the derived class, it automatically executes the parent's method, is
it possible?

// main code:
Derived d = new Derived();
d.SomeMethod();


public class Base
{
public virtual void SomeMethod() {
Console.WriteLine "A";
}
}

public class Derived : Base
{
public override void SomeMethod() {
// this.parent.SomeMethod();
Console.WriteLine "B";
}
}

Is there a way to call the derived class and then both "A" and "B" will be
printed out?









---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 

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