473,508 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a parent class method

I have a class (sharedClass) that is instantiated (not derived) by two other
classes (guiClassA and guiClassB). The sharedClass needs to be able to call
a method that is defined in its parent class (guiClass).

Specifically, when guiClassA or guiClassB calls a method from the
sharedClass, the sharedClass needs to log its actions by sending strings to
the guiClass (via guiClass method calls), which in turn display or record
this log information in their own unique ways.

How can the sharedClass call out a function from its parent class without
knowing exactly what kind of class the parent class is? (I can however
guarantee that the parent class will implement the logging function).
Since both guiClasses are not used in the same application, I was able to
solve this in C++ by declaring the logging function prototype in a header
file that was included by all the classes, and then defining the function
implementation at the same level as the guiClass was instantiated. Thus, the
connection was made by the linker. Is there any way to do something similar
in C#?

Nov 17 '05 #1
1 2334
I was going to suggest declaring the logging function(s) as an
interface implemented by both gui classes, but I just thought of a
better way.

Don't use methods at all. Use events. Have your sharedClass expose a
(or multiple) shared event(s). You can declare your own event arguments
and your own event handler type to carry any specialized information
you need. Whenever it needs to log anything, the sharedClass simply
raises the appropriate event.

It's then up to the enclosing guiClass to subscribe to the event(s) and
do whatever it needs to do (log the event) as appropriate.

I wouldn't even name the event according to what has to happen (don't
call it LogXXXX). Instead, I would name the event according to what
happened that was "loggable". You know: ValueChanged,
CertificateExpired, whatever. This may lead to multiple events from the
sharedClass, but this also achieves maximum decoupling, both logically
and conceptually. The sharedClass just notifies the "outside world"
when certain things happen and provides details about the event. The
guiClass is then just one interested party that happens to respond to
those events by logging them. Other classes may want to subscribe to
the events for other reasons.

So... I would use a completely different paradigm: events rather than
callbacks.

Nov 17 '05 #2

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

Similar topics

4
9615
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
14
6378
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...
3
11425
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...
6
10962
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;...
6
17550
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...
1
12314
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...
5
3396
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...
1
6508
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...
1
2500
by: Ryan Krauss | last post by:
I am trying to call a parent's __init__ method from the child's: class ArbitraryBlock(InnerBlock): def __init__(self, codelist, noout=False, **kwargs): InnerBlock.__init__(self, codelist,...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
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...
0
7321
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,...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4705
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
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 ...
1
762
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.