473,657 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invoking base class event from the method of derived class method

21 New Member
hi every body
i write a code in c sharp in which i have made a base class and make an event there.Then i make a derived class and made an other event in that class.
Now what i wanna do is that i wanna invoke in derived class method the base class event but i am facing an error there i am unable find the solution.canaby body tell me the solution.follow ing is the code.



Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace derivedevents
  7. {
  8.     delegate void eventhandler();
  9.     class bclass
  10.     {
  11.         public  event eventhandler handler;
  12.         public void furqan()
  13.         {
  14.             Console.WriteLine("furqan");
  15.         }
  16.         public void furqan1()
  17.         {
  18.             Console.WriteLine("ahmed");
  19.         }
  20.         public void fire()
  21.         {
  22.             handler();
  23.         }
  24.  
  25.     }
  26. }
  27.  
  28. //now the base class
  29. namespace derivedevents
  30. {
  31.     delegate void eventhandler1();
  32.     class dclass:bclass 
  33.     {
  34.         public event eventhandler1 handler1;
  35.         public void ahmed()
  36.         {
  37.             Console.WriteLine("ahmed");
  38.         }
  39.         public void ahmed1()
  40.         {
  41.             Console.WriteLine("ahmed ahmed");
  42.         }
  43.         public void fire1()
  44.         {
  45.             handler1();
  46.             handler();\\ i am actually facing the problem here facing error       
  47.         }
  48.  
  49.  
  50.  
  51.  
  52.     }
  53. }
Dec 23 '09 #1
5 3733
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Dec 23 '09 #2
RobertPerona
2 New Member
To fix your immediate problem you could change your fire1 method in derived class to the following.

Expand|Select|Wrap|Line Numbers
  1.         public void fire1() 
  2.         { 
  3.             handler1(); 
  4.             base.fire();         
  5.         }
I would recommend checking out the following link for more on the suggested form of events in c#
MSDN: Raising Base Class Events
MSDN: Event Design
Dec 24 '09 #3
furqi
21 New Member
i have also used base keyword but it is also giving me an error
any other suggestion please
Dec 24 '09 #4
RobertPerona
2 New Member
Please post the error you are getting. Also any additional code, for example the code you are using to instantiate and call the class if you are getting a runtime exception.

In addition for safety in case of execution of unattached events any code invoking the event should follow the following pattern, outlined in previous links I posted.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public void fire1()  
  3.         {  
  4.             if(handler1 != null)
  5.             {
  6.                 handler1();  
  7.             }
  8.             base.fire();          
  9.         } 
Notes
  1. This would also apply to your fire method in the base class, test for handler to be null before invoking.
  2. Normally the event execution method would be named On<Event Name> in you code being onHandler.
Dec 24 '09 #5
tlhintoq
3,525 Recognized Expert Specialist
Another example
http://bytes.com/topic/c-sharp/insig...le#post3528708
Dec 24 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7331
by: mrstephengross | last post by:
Ok, I've got code that looks something like this: ================================================== template<typename T1, typename T2> class Base { public: explicit Base(const T1 & t1) { /* ... */ } };
13
2573
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the original System.Web.UI.Page in order to have common checks in the page base. i make securirty checks in the page base page_load event. if the security fails, i can do whatever i want before the "real" / derived page gets to be executed.
5
3153
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
4
2844
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
12
3429
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
6
10535
by: ketan | last post by:
Hi All, I have a situation where in I need to call static methods of the derived class. I checked previous posts, but could not get any satisfactory reply. My situation can be simulated with following code. // // Does not compile. Not correct C++ syntax - shows what needs to be
5
2618
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
3
2640
by: Bit Byte | last post by:
Must be the time of the day, but I seem to get my head in a spin over this ... I have a base class B, from which I have a derived class. In class B, i have a (public access) typedef of a template class functor (lets call it F for convenience) I want to declare a variable of type F in class D.
6
2814
by: Charles Law | last post by:
I have a base class and derived classes that relate to a set of documents I process. e.g. DocBase, DocA, DocB, DocC. The processing of each document is handled in teh derived classes, as you might imagine, but the base class has properties and methods that are common to all documents. The processing of each docuemnt can result in a range of document specific errors, which I want to raise as events on the document object, but there are...
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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
8739
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
8512
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,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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
5638
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
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
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.