473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

invoking base class event from the method of derived class method

21
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.following 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 3712
tlhintoq
3,525 Expert 2GB
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
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
i have also used base keyword but it is also giving me an error
any other suggestion please
Dec 24 '09 #4
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 Expert 2GB
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
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
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...
5
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...
4
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. ...
12
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...
6
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...
5
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
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...
6
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.