473,388 Members | 1,220 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,388 software developers and data experts.

Class Creation with Events

Can anyone point me in the direction of creating an event in a class I
develop? What I am looking for is finding a way that when a property gets
set or is changed a method is automatically run to perform validation or
anything else. Is it possible to put code in the get/set of properties?

Thanks,

Matt
Nov 16 '05 #1
2 1118
Matt,
Do you just want to call a method, or do you want to trigger an event?
Either way I suppose could be done:

private string _myProperty = null;

public string MyProperty
{
get{return(this._myProperty);}
set
{
this._myProperty = value;
//make method call or trigger event here.
}
}

--
Lateralus [MCAD]
"Matt" <md*****@sorvive.DONT-SEND-SPAM.com> wrote in message
news:eg**************@tk2msftngp13.phx.gbl...
Can anyone point me in the direction of creating an event in a class I
develop? What I am looking for is finding a way that when a property gets
set or is changed a method is automatically run to perform validation or
anything else. Is it possible to put code in the get/set of properties?

Thanks,

Matt

Nov 16 '05 #2
Hi there...

Yes it's possible... for example

using System;

class Test {
public delegate void MyDelegate(Object sender, DateTime time, int value);
public event MyDelegate OnFireMyEventSetterCalled;
public event MyDelegate OnFireMyEventGetterCalled;

private int m_count;

public int Count {
get {
if (OnFireMyEventGetterCalled != null)
OnFireMyEventGetterCalled(this, DateTime.Now, m_count);
return m_count;

}

set {
m_count = value;
if (OnFireMyEventSetterCalled != null)
OnFireMyEventSetterCalled(this, DateTime.Now, m_count);

}
}
}
class EntryPoint {

static void Main() {
Test x = new Test();
x.OnFireMyEventSetterCalled += new Test.MyDelegate(Setter);
x.OnFireMyEventGetterCalled += new Test.MyDelegate(Getter);
Console.WriteLine("Insert an integer: ");
x.Count = int.Parse(Console.ReadLine());
Console.WriteLine(x.Count);
Console.ReadLine();
}

static void Setter(Object sender, DateTime time, int value) {
Console.WriteLine("\nSetter Called!!!");
Console.WriteLine("Sender :"+sender.ToString());
Console.WriteLine("Time :"+time.ToString());
Console.WriteLine("value :{0}",value);

}

static void Getter(Object sender, DateTime time, int value) {
Console.WriteLine("\nGetter Called!!!");
Console.WriteLine("Sender :"+sender.ToString());
Console.WriteLine("Time :"+time.ToString());
Console.WriteLine("value :{0}",value);
}

}
Regards,
--
Angel J. Hernández M
MCSD


"Matt" <md*****@sorvive.DONT-SEND-SPAM.com> escribió en el mensaje
news:eg**************@tk2msftngp13.phx.gbl...
Can anyone point me in the direction of creating an event in a class I
develop? What I am looking for is finding a way that when a property gets
set or is changed a method is automatically run to perform validation or
anything else. Is it possible to put code in the get/set of properties?

Thanks,

Matt

Nov 16 '05 #3

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

Similar topics

13
by: Stuart McGraw | last post by:
I haven't been able to figure this out and would appreciate some help... I have two tables, both with autonumber primary keys, and linked in a conventional master-child relationship. I've...
5
by: Martin Maat [EBL] | last post by:
Hi, I have a GUI application that responds to events coming from an externaql application. In the implementing code for the event I need to create a GUI control and "parent" the control into the...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
12
by: Riaan | last post by:
Hi Everybody, I created my own class with 2 methods (both public) in it. When I use the 1 method it works fine, as soon as I make a call to the other method, the program crashes. I removed all...
4
by: pigeonrandle | last post by:
Hi, Funnily enough, i have a question about passing data between classes. There's an excellent diagram below to help drive away any confusion. I have class A which contains an instance of class...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
0
by: BLUE | last post by:
I have a singleton Operation class representing current user operation. Each time the user make an operation it is saved by the DAL on an XML file. There is a timer whose tick event handler should...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
0
by: =?Utf-8?B?YV93YWhvbw==?= | last post by:
I am developing a diagramming project within C# using WPF. I based the visual representation off a tutorial from CodeProject <a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
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
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...

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.