473,669 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling parent class?

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.WriteLi ne "A";
}
}

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

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


Nov 18 '05 #1
1 1319
public class Derived : Base
{
public override void SomeMethod() {
base.SomeMethod (); // note lower case 'b'
Console.WriteLi ne "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.publi c.dotnet.langua ges.csharp/<vr************ **@news04.bloor .is.net.cable.r ogers.com>

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.WriteLi ne "A";
}
}

public class Derived : Base
{
public override void SomeMethod() {
// this.parent.Som eMethod();
Console.WriteLi ne "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.publi c.dotnet.langua ges.csharp]
Nov 18 '05 #2

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

Similar topics

14
6399
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
2
1908
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
3
11436
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function does somthing that must be done. This meens that when a child class inherits the class and creates its own vertual int A() the parent class must also be called. the prob is i can not use the base class name and then its functino name after it...
6
10968
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str; public: std::string& getStr(){return str;}
6
17564
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make my main form the startup object, I'd rather put my Main() function in a class or code file. Why? Because thats what I always do in standard C++ or VB. In my opinion, writing all my application logic in the form itself is sloppy and only for...
1
12323
by: NOSPAM | last post by:
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
5
3414
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
12
1975
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
1
6526
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the Talker() in Parent will call the Talker() method in every child class. The kicker is that I have many different Child classes, and not all Child classes will be loaded when Talker() in the Parent is called. Thanks, Randy
7
1573
by: angelajean | last post by:
Hi all, I have a problem here that I am not sure how I should go about solving. I am thinking of creating classes that iteratively calls on other classes such that at the end of the day, I can get something like: Criteria.Subcriteria.Subcriteria.Subcriteria.value = a; So it seems like at first instance, I have a Subcriteria class: class Subcriteria {
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.